Learn to Git Cherry Pick a Commit From Scratch

GitDecember 10, 2019

Sometimes you don’t want to merge two branches, but simply your desire is to apply the same changes in a commit on top to another branch. This situation is very common when working on bugs: you fix a bug in a branch, and then you want to apply the same fix on top of another branch.

Git has a convenient way to do it; this is the git cherry-pick command.

Let’s play with it a little bit.

Assume you want to pick the blackberry from the berries branch, and then apply it into the master branch; this is the way:

// In your gatsby-config.js
plugins: [
  {
    resolve: `gatsby-transformer-remark`,    options: {
      plugins: [`gatsby-remark-prismjs`],    },
  },
]
src/index.js
[1] ~/grocery (master)
$ git cherry-pick ef6c382
error: could not apply ef6c382... Add a blackberry
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

For the argument, you usually specify the hash of the commit you want to pick; in this case, as that commit is referenced even by the berries branch label, doing a git cherry-pick berries would have been the same.

Okay, the cherry pick raised a conflict, of course:

[2] ~/grocery (master|CHERRY-PICKING)
$ git diff
diff --cc shoppingList.txt
index 862debc,b05b25f..0000000
--- a/shoppingList.txt
+++ b/shoppingList.txt
@@@ -1,5 -1,4 +1,9 @@@
  banana
  apple
  orange
++<<<<<<< HEAD
 +peach
- grape
++grape
++=======
+ blackberry
++>>>>>>> ef6c382... Add a blackberry

The fourth line of both the shoppingList.txt file versions has been modified with different fruits. Resolve the conflict and then add a commit:

[3] ~/grocery (master|CHERRY-PICKING)
$ vi shoppingList.txt
[4] ~/grocery (master|CHERRY-PICKING)
$ git add shoppingList.txt

[5] ~/grocery (master|CHERRY-PICKING)
$ git status
On branch master
You are currently cherry-picking commit ef6c382.
  (all conflicts fixed: run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:

modified:   shoppingList.txt

Note the git status output: you always have some suggestions; in this case, to abort a cherry-pick and undo all you did, you can do a git cherry-pick —abort (you can do the same even while rebasing or merging).

Now go on and commit:

src/index.js
[6] ~/grocery (master)
$ git commit -m "Add a cherry-picked blackberry"
On branch master
nothing to commit, working tree clean

[7] ~/grocery (master)
$ git log --oneline --graph --decorate --all
* 99dd471 (HEAD -> master) Add a cherry-picked blackberry
* 6409527 Add a grape
* 603b9d1 Add a peach
| * a8c6219 (melons) Add a watermelon
| * ef6c382 (berries) Add a blackberry
|/
* 0e8b5cf Add an orange
* e4a5e7b Add an apple
* a57d783 Add a banana to the shopping list

Okay, as you can see a new commit appeared, but there are no new paths in the graph. Unlike the merging feature, with cherry-picking you only pick changes made inside the specified commit, and no relationship will be stored between the cherry-picked commit and the new one created.

If you want to track what was the commit you cherry-picked, you can append the -x option to the git cherry-pick command; then, while committing, don’t append the message in the git commit command using the -m option, but type git commit and then press ENTER to allow Git to open the editor: it will suggest you a message that contains the hash of the cherry-picked commit.

This is the only way to track a cherry pick, if you want.


Join the newsletter

I write about modern JavaScript and programming. Once a month, I’ll send you a tutorial that’s guaranteed to help you become a better developer. No unexplained jargons. Just plain good content. One-click unsubscribe anytime.

Subscribe to Newsletter