Git Copying a commit from one branch to another


Example

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]

Where the new commit a66b23 has the same content (source diff, commit message) as b886a0 (but a different parent). Note that cherry-picking will only pick up changes on that commit(b886a0 in this case) not all the changes in feature branch (for this you will have to either use rebasing or merging).