Copying a commit from one branch to another

suggest change

git cherry-pick <commit-hash> will apply the changes made in an existing commit to another branch, while recording a new commit. Essentially, you can copy commits from branch to branch.

Given the following tree (Source)

dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
           \
            76cada - 62ecb3 - b886a0 [feature]

Let’s say we want to copy b886a0 to master (on top of 5a6057).

We can run

git checkout master
git cherry-pick b886a0

Now our tree will look something like:

dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 - a66b23 [master]
           \
            76cada - 62ecb3 - b886a0 [feature]

The new commit a66b23 has the same change and commit message as b886a0, but a different parent. Cherry-pick applies only that commit, not the rest of feature. To bring the whole branch over, merge or rebase it. Cherry-pick needs a clean enough working tree to apply the patch; if the change does not apply, Git stops and leaves conflicts.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents