The Git Cherry-pick and Git Rebase Interactive Combo

Puzzle pieces on a light grey background

Version control is necessary, but using it well is an art. Today I am going to give an overview of two Git commands that I found helpful during software development, git cherry-pick and git rebase in interactive mode.

Git Cherry-pick

Software development is often a team game. To work within the planned schedule, each developer will implement features in their own feature branches. But sometimes there are changes that everyone wants! git cherry-pick is a Git command that lets the developer pick a commit and apply it their branch. This command comes in handy when you want to use features implemented by other developers but your branch isn’t built off their branch. This comes in handy when you need just one change they made, but don’t want all of their changes right now.

As an example, consider this scenario: an existing bug made it into the main branch. A developer solves it in their branch. They’re still working, but you’re done. Instead of waiting for the fix to make it into the main branch, or creating an entirely new branch just for the fix, you can “cherry-pick” the commit that fixes the bug into your branch.

# Check out the branch you are interested in cherry-picking from
% git checkout <interested-feature-branch>

# Look for the commit SHA of the commit you want to cherry-pick
# Note: Press enter/return to scroll down the list and press q to exit
% git log

# Check out your branch
% git checkout <your-feature-branch>

# Cherry-pick the commit to your branch
% git cherry-pick <commit-sha>

Git Rebase Interactive

While cherry-picking can be valuable, it can also cause duplicate commits and can lead to unnecessary code conflicts during pull requests. To avoid this, we can “squash” the commits before creating a pull request. To do this, git rebase in interactive mode. Unlike the standard git rebase that rebases all commits to the desired branch, you have control over your commit history with an interactive git rebase.

# Check out your branch
% git checkout <your-feature-branch>

# Rebase interactively on another branch, e.g. master
% git rebase -i <another-branch>
# You can also use % git rebase --interactive <another-branch>

git rebase -i will list the commits on the current branch in a vim editor. You can remove or squash commits here.

The following is an example after calling git rebase -i .

pick 7cb4839 Old commit 1
pick e616408 Duplicate commit
pick 79bd7a2 Old commit 2
pick fcaf152 fixup! Old commit 2
pick bdcd3db Add new feature

# Rebase 0462d8f..bdcd3db onto 0462d8f (1 command)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#

If you are new to vim, you can go to the duplicate commit line and press d twice.

“Squashing” keeps the work on a commit but adds the changes to the commit before it. If you want to squash the fixup! Old commit 2 commit to the Old commit 2 commit, you can put your cursor over squash in the comments and type yiw, then put it over to the pick before the fixup! Old commit 2 commit and replace it by typing viwp.

# VIM Definition
yiw = yank inner word (inner = the word your cursor is on)
viwp = visual + inner word + paste

Now, the pick turns into squash as seen here:

pick 7cb4839 Old commit 1
pick 79bd7a2 Old commit 2
squash fcaf152 fixup! Old commit 2
pick bdcd3db Add new feature

When you are satisfied with the changes, press esc to make sure you are out of any mode and enter :wq to save and continue rebasing. If there are merge conflicts, resolve them, and then use git rebase --continue to finish the rebase. Once you see the following message, you can now push the commits to your remote.

Successfully rebased and updated refs/heads/<your-feature-branch>.

With these Git commands, your commit history will look clean and clear while working in a team.

If you want to learn even more git skills, consider reaching out! At DojoFive, we have talented engineers on hand ready to help you with all aspects of your EmbedOps journey. We are always happy to help with interesting problems that need solving. You can reach out at any time on LinkedIn or through email!

Sign up to get our content updates!

Unlock the full potential of your embedded projects with our expert insights! Dive into our comprehensive resources to stay ahead in firmware development. Subscribe now to get the latest best practices and guides delivered straight to your inbox.

Sign Up for Updates

Discover why Dojo Five EmbedOps is the embedded enterprise choice for build tool and test management.

Sign up to receive a free account to the EmbedOps platform and start building with confidence..

  • Connect a repo
  • Use Dev Containers with your Continuous Integration (CI) provider
  • Analyze memory usage
  • Integrate and visualize static analysis results
  • Perform Hardware-in-the-Loop (HIL) tests
  • Install the Command Line Interface for a developer-friendly experience

Subscribe to our Monthly Newsletter

Subscribe to our monthly newsletter for development insights delivered straight to your inbox.

Interested in learning more?

Best-in-class embedded firmware content, resources and best practices

Laptop with some code on screen

I want to write my first embedded program. Where do I start?

The boom in the Internet of Things (IoT) commercial devices and hobbyist platforms like the Raspberry Pi and Arduino have created a lot of options, offering inexpensive platforms with easy to use development tools for creating embedded projects. You have a lot of options to choose from. An embedded development platform is typically a microcontroller chip mounted on a circuit board designed to show off its features. There are typically two types out there: there are inexpensive versions, sometimes called

Read More »
Medical device monitoring vitals

IEC-62304 Medical Device Software – Software Life Cycle Processes Primer – Part 1

IEC-62304 Software Lifecycle requires a lot of self-reflection to scrutinize and document your development processes. There is an endless pursuit of perfection when it comes to heavily regulated industries. How can you guarantee something will have zero defects? That’s a pretty hefty task. The regulatory approach for the medical device industry is process control. The concept essentially states that if you document how every step must be completed, and provide checks to show every step has been completed properly, you

Read More »
Operating room filled with medical devices

IEC-62304 Medical Device Software – Software Life Cycle Processes Primer – Part II

Part I provides some background to IEC-62304. Part II provides a slightly more in-depth look at some of the specifics. The IEC 62304 Medical Device Software – Software Lifecycle Processes looks into your development processes for creating and maintaining your software. The standard is available for purchase here. So what activities does the standard look at? Here are some of the major topics. For any given topic, there will be a lot more specifics. This will look at a few

Read More »