Git Cherry Pick

Only want to bring one specific commit or a range of commits from another branch onto current working branch. Note that git merge and git rebase will bring all differences.

Cherry-pick Reference git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong. Note the -n flag means to not create commit but staging the picked content.

Check if it can be done by UI first.

1
2
3
4
5
6
7
# go to from branch to get target commit sha
git log -n 10

git checkout <to branch>
# --no-commit: just pick not creating commit
# -edit: edit commit message before commit
git cherry-pick <commit sha> [--no-commit] [-edit]

If there are conflicts after git cherry-pick, resolve the conflicts first before next cherry pick operation. As this comment mentioned, you can cherry pick multiple commits all in one go, so you don’t have to resolve conflicts for changes that are undone by later commits.

0%