HomeToolsAbout

Cherry-pick

Strategy

Atomic commits really help cherry picking.

  • Before merging a branch into main, it may be worth it to squash the commits so the cherry-picking becomes easier.

When you have multiple commits that you need to apply to a separate release branch with mixed commit history, you need to cherry-pick individual commits in sequential order to apply them correctly to the release branch.

# HEAD at release branch git cherry-pick commit_sha_1 # from main branch git cherry-pick commit_sha_2 # from main branch git cherry-pick commit_sha_3 # from main branch

How Cherry-pick works

When you want to apply a single commit from another branch into current branch.

gitGraph
  commit
  commit
  branch develop
  checkout develop
  commit
  commit id: "123"
  commit
  checkout main
  commit
  cherry-pick id: "123"
  commit
graph LR
  A1["a"] --> A2["b"]
  A2 --> A3["c"]
  A3 --> A4["d"]

Bring in Specific Commit from Remote

You cannot pull specific commit from a remote; you can instead fetch the data and merge or cherry-pick

git fetch origin

View the code at specified commit_sha or branch name (if tip of branch includes the commit you want):

git checkout commit_sha

Bring only the commit_sha to the destination_sha branch:

git checkout destination_sha && git cherry-pick commit_sha

Bring all the changes leading up to commit_sha to the destination_sha branch:

git checkout destination_sha && git merge commit_sha
AboutContact