CH 27 / 75Part 4: Branches and Merging

Bringing Over a Specific Commit with cherry-pick

What you will learn on this page

  • Explain cherry-pick as an operation that "copies one commit's change onto the branch you're on"
  • Find the ID of the commit you want to bring over with git log main..
  • Record which commit it was copied from with -x
  • Know when to use cherry-pick and when to avoid it
On this page9 items

In #23–#26, we brought a branch’s commits into main all together. Sometimes, though, a branch contains just one commit that you want “on main right now.” This page covers cherry-pick, which lets you pick one commit and bring it over.

cherry-pick “copies a change”#

cherry-pick means “picking cherries one at a time.” It’s an operation that chooses one commit from another branch and copies just its change onto the branch you’re on as a new commit. It doesn’t bring in the whole branch.

In the practice repository, let’s create a branch for planning meals. Suppose that along the way, an addition to the shopping memo that has nothing to do with the meal plan got mixed in.

text10 lines
cd ~/git-practice
git switch -c menu-plan
echo "Monday curry" > menu.txt
git add menu.txt
git commit -m "Create the meal plan"
echo "Buy butter" >> shopping.txt
git commit -am "Add butter to the shopping memo"
echo "Tuesday pudding" >> menu.txt
git commit -am "Add Tuesday to the meal plan"
git switch main

The meal plan is still a work in progress, so you can’t bring in the whole branch. But you’re going to buy butter today, so you want just the shopping memo change on main. This is exactly when cherry-pick comes in.

Find the commit to bring over, and copy it#

First, look up the ID of the commit you want to bring over.

text1 lines
git log --oneline main..menu-plan
text3 lines
a858a32 Add Tuesday to the meal plan
d2feb3d Add butter to the shopping memo
e46ec1d Create the meal plan

main..menu-plan, which we used in #26, is the range of “commits in menu-plan that aren’t in main.” The one you want is d2feb3d. Make sure you’re on main, and specify the ID.

text1 lines
git cherry-pick d2feb3d
text3 lines
[main 40c666c] Add butter to the shopping memo
 Date: Tue Oct 27 23:34:20 2026 +0900
 1 file changed, 1 insertion(+)

A new commit, 40c666c, has been created on main. The Date: line shows that the copied commit inherits the creation date and time of the original commit.

text1 lines
git log --oneline --decorate --graph --all -n 5
text6 lines
* 40c666c (HEAD -> main) Add butter to the shopping memo
| * a858a32 (menu-plan) Add Tuesday to the meal plan
| * d2feb3d Add butter to the shopping memo
| * e46ec1d Create the meal plan
|/  
* 93a4042 Change potatoes to 3
Separate from main, the menu-plan branch has three commits: create the meal plan, add butter to the shopping memo, and add Tuesday to the meal plan. git cherry-pick d2feb3d copies only the butter commit's change onto main as a new commit, deb9a28
cherry-pick copies only the chosen commit's change as a new commit

The same “Add butter to the shopping memo” now exists on both main (40c666c) and menu-plan (d2feb3d). Nothing has changed on menu-plan. And menu.txt is not on main.

text1 lines
ls
text6 lines
config.txt
debug.log
recipes
shopping.txt
tmp
todo.txt

config.txt, debug.log and tmp are files we excluded in Part 2, #12 and #13, so they stay in the working tree regardless of branch.

Record where it came from: -x#

40c666c and d2feb3d are different commits, even though their contents are the same. So that you can tell later “where this was copied from,” it’s a good idea to add -x. Let’s undo the current commit and redo it with -x.

text3 lines
git reset --hard HEAD~1
git cherry-pick -x d2feb3d
git log -n 1 --format='%h%n%B'
text8 lines
HEAD is now at 93a4042 Change potatoes to 3
[main deb9a28] Add butter to the shopping memo
 Date: Tue Oct 27 23:34:20 2026 +0900
 1 file changed, 1 insertion(+)
deb9a28
Add butter to the shopping memo

(cherry picked from commit d2feb3d24ec6092696377f1c560a3ddd57acf11f)

(cherry picked from commit …) has been appended to the end of the message. --format='%h%n%B' displays the short ID and the full message (Part 2, #11).

Check whether something has already been copied#

You can check which of menu-plan’s commits have already been copied to main with git cherry.

text1 lines
git cherry -v main menu-plan
text3 lines
+ e46ec1df73e8df735dcd553d4cf757d4432c1e19 Create the meal plan
- d2feb3d24ec6092696377f1c560a3ddd57acf11f Add butter to the shopping memo
+ a858a32716fe51948ae4218e0e6c4b963d258edd Add Tuesday to the meal plan

- means “the same change is already on main,” and + means “not yet.” Even though the IDs differ, it decides by comparing the content of the changes.

Cleaning up, and when there’s a conflict#

This time, we’ll throw the meal plan away instead of bringing it in.

text1 lines
git branch -d menu-plan
text3 lines
error: the branch 'menu-plan' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D menu-plan'
hint: Disable this message with "git config set advice.forceDeleteBranch false"

Even though we copied the butter commit, menu-plan’s commits themselves aren’t on main, so -d refuses (#22). We’ve decided to throw it away, so delete it with -D.

text1 lines
git branch -D menu-plan
text1 lines
Deleted branch menu-plan (was a858a32).

With cherry-pick too, if the change being copied clashes with the content of the branch you’re on, you get a conflict. How to fix it when it stops is the same as in #24: editing the file and running git add are the same. After that, you use the cherry-pick-specific commands.

CommandWhat it does
git cherry-pick --continueCreate the commit with your fixes
git cherry-pick --skipStop copying this commit and move on to the next (when copying several)
git cherry-pick --abortReturn to the state before you started the cherry-pick

When to use it, and when to avoid it#

Good times to use itBetter to avoid
You want just an urgent fix from a work branch on main firstYou want to bring over many commits from a branch (use merge or rebase)
You committed on the wrong branch and want to move it to the right oneYou plan to merge that whole branch later anyway
You want just a bug fix made on main in an older version’s branch

A commit copied with cherry-pick is “a different commit with the same change,” so if you merge the original branch later, the same change appears twice in the history. Git usually layers them nicely, but if you’ve changed the same lines again afterward, it can cause conflicts. cherry-pick is the tool for when you want “just that one,” and connecting branches to each other is the job of merge and rebase.

FAQ#

Q. Can I cherry-pick several commits at once?
A. Yes. List them like git cherry-pick ID1 ID2 and they’re copied in order. Specify a range like git cherry-pick A..B to copy from the one after A through B (A itself isn’t included). But if you’re bringing over a lot, consider merge or rebase.

Q. Can I bring just the changes into the working tree without committing?
A. With -n (--no-commit), the changes are only applied to the working tree and staging area, and no commit is created. Use it when you want to check the contents and commit yourself.

Summary#

  • cherry-pick chooses one commit from another branch and copies its change onto the branch you’re on as a new commit
  • Find the ID of the commit you want with git log --oneline main..<branch>, then stand on the receiving branch and run git cherry-pick ID
  • The copied commit is a different commit with a different ID. It’s good to record its origin with -x
  • git cherry -v tells you whether something has already been copied
  • Connecting branches is the job of merge and rebase. Use cherry-pick when you want “just that one”

Next page#

28. Branching strategies (GitHub Flow / Git Flow / trunk-based): To wrap up Part 4, we compare three sets of team rules for how to create branches and how to bring them in.