Copy one commit to another branch with cherry-pick

Description: The branch develop has an unfinished feature and a bug fix. Bring only the bug fix commit to main.

Hint:

  • Find the commit with git log, then use git cherry-pick with its name.

Solution

Terminal output

$ git log –oneline develop
0bbe41e (develop) Fix header bug
c03680a Add feature (not ready)
1e3d759 (HEAD -> main) Add home
$ git cherry-pick develop
[main 97e3b7d] Fix header bug
Date: Sat Jan 10 11:31:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 fix.txt
$ git log –oneline
97e3b7d (HEAD -> main) Fix header bug
1e3d759 Add home
$ ls
fix.txt
home.txt

How it works

  • The fix is the latest commit of develop, so we can use the branch name. Otherwise use the hash.
  • Only fix.txt came to main. The unfinished feature.txt did not.

Read Git Cherry Pick.


Learn more about the similar topics:
Tutorials
Git Branch
Git Checkout and Switch
Git Merge
Git Merge Conflict
Git Rebase
Git Cherry Pick
Git Branching Strategy
Exercises & Assignments
Create a new branch and switch to it
Merge a feature branch into main
Resolve a merge conflict
Delete a merged branch
Rename a local branch
Copy one commit to another branch with cherry-pick
Interview Questions & Answers
How you can see list of all branches in Git?
How do I create a new branch in Git?
How to switch to an another branch in Git?
How to merge one branch to another in Git?
How to rename a local Git branch?
What is the difference between git merge and git rebase?
What is a merge conflict and how do you resolve it?