As beginning developers, we learn git simply through repetition. We learn quickly what git pull, git push, and git commit each means. As we transition to work on bigger projects and collaborate with teams, we start to learn more advanced git commands that will help keep our codebase’s version control history straight between the individuals who work on a project.
One such command is git cherry-pick . The git cherry-pick command is used when we want to take specific commits from one branch and attach them to the HEAD of another feature branch or the master branch.
What is git cherry-pick?
Think of cherry-picking as working on a group project. Each person has a specific section he or she needs to work. At the end, they will combine each section into one total project. You might take parts of a certain section and splice it to a part in another section so the project will flow better.
This is in essence what git cherry-pick is: we take a commit or multiple commits from one feature branch and attach it as a new commit to another branch. Let’s take a look at how it works:
This basic diagram represents two branches from a project that is tracked by git. The letters represent different commits made to the git repository on their respective branches. The dashes represent the history, from oldest to youngest. Try to imagine the structure of the two branches and how they work when following these steps:
Steps to complete git cherry-pick
Think of each git commit or letter (as illustrated above) as a cherry. Each cherry has a unique hash associated with it – if you’re not sure what a hash is, think of it as a fingerprint or unique identifier that’s associated with the commit. We’ll need that hash in order to pick that cherry and add it to another branch.
- git checkout
<name of branch you’d like to grab commit from>
Checkout to the branch you would like to pick your commit/cherry from. - git reflog
The git reference log,reflog
, keeps track of recent actions made. Here is an example of a git reflog:% git reflog
bf654bb (HEAD -> master, origin/master) HEAD@{0}: commit: last commit message made
2394353 HEAD@{1}: commit: where head was 2 commit messages ago
b4b51eb HEAD@{2}: commit: where head was 3 commit messages ago
Your commit reference log, if working with a large team, may have more information, such as the date, time, and/or author who made the commit. Here, though, we see the commit hash, the branch where the commit was made, the action made, and the actual commit message.
Once you know which commit you would like to add to the other branch, take note of the hash – the hash is the string of numbers and characters at the beginning of each line in this example. - git checkout <name of branch you would like to add commit to>
After you have made note of the hash, switch to the branch you would like to add the commit to. - git cherry-pick [-x] <commit hash>
Use the git cherry-pick command with the commit hash to add the commit to that branch’s working tree. Use the -x flag when you are cherry-picking from a public branch as this will append a line that remarks the original commit it was cherry-picked from.
Let’s take another look at our diagram after the cherry-pick:
Let’s take another look at our diagram after the cherry-pick:
Our “cherry”, or commit hash, that we chose in this example is “C”. After following the steps outlined above, the selected commit, “C” in this instance, is added as a commit to the history of the second branch.
Final Notes
The last thing to do is to copy any notes over from the cherry-picked commit. Use git notes copy <cherry-picked commit hash> <new-commit-hash>
to copy over any notes that were made in the original commit.
Also, be sure to resolve any merge conflicts that might arise as a result.
In this article, we took a look at the process it takes to use an advanced git command called git cherry-pick. Use it when you need to take a commit from one branch and add it to another branch.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.