git cherry-pick is a simple method for applying the same changes into multiple branches. It’s enough to do changes once, then copy commit into different places.

Find commit id

To select only one commit, first find it’s id. Depending on tool there are different methods to do so:

  • Check history in gui tool (Source Tree, GIT GUI, GIT Kraken, etc…)
  • Check history in command line: git log for example
  • Check history in remote tool (GitHub, GitLab, Gerrit, etc…)

Pick one commit

Switch to destination branch and pass commit id to git cherry-pick command. Commit id can be either short or full.

# git cherry-pick COMMIT_ID

git cherry-pick e1d3c7
# OR
git cherry-pick e1d3c7d0d51d59de61020517a2b53119ead4b7d6

In case of conflicts (it happens), follow the same procedures as during merge, rebase, or other operations that moves commits between branches.

Pick multiple commits

It’s very common practice to create multiple small commits for each feature or task. In such situation it is very time consuming to pick commit by commit.

Fortunately, git cherry-pick supports ranges!

Lets suppose that source branch looks like this:

Last commit (HEAD)
  |
  Z
  |
  Y
  |
  X
  |
 ...
  |
  D
  |
  C
  |
  B
  |
  A
  |
Initial Commit

Now we need to pick commits starting from C up to Y. There are 2 ways of defining range:

  • Including all pointed commits: C^..Y
  • Starting from next commit than given: B..Y - take next commit (C) than given (B)
# Remember about switching to destination branch

git cherry-pick C^..Y
# OR
git cherry-pick B..Y

https://git-scm.com/docs/git-cherry-pick https://stackoverflow.com/questions/1994463/how-to-cherry-pick-a-range-of-commits-and-merge-them-into-another-branch
⤧  Next post AWK split input by… ⤧  Previous post Pipes in shell