Privacy Policy
Snippets index

  Cherry-picking a GIT commit from another git repository

This script applies locally the diff of a commit from another repo; "apply" doesn't commits the patch, so you have to supply a commit message yourself

git --git-dir=../[some_other_repo]/.git show [commit_SHA] | git apply

The following script also commits the patch:

git --git-dir=../[some_other_repo]/.git format-patch -k -1 --stdout [commit_SHA] | git am -3 -k --ignore-whitespace

Explanation:

  • The git format-patch command creates a patch
  • from [some_other_repo]'s commit
  • specified by its [commit_SHA]
  • "-1" for one single commit alone
  • "-k" keeps the subject (don't add [PATCH])
  • This patch is piped to git am, which applies the patch locally
  • "-3" means trying the three-way merge if the patch fails to apply cleanly
  • "-k" keeps the subject (don't strip [PATCH])

Other options:

-edit: will cause git to prompt for a commit message before applying the cherry-pick operation
--no-commit: will execute the cherry pick but instead of making a new commit it will move the contents of the target commit into the working directory of the current branch
--signoff: will add a 'signoff' signature line to the end of the cherry-pick commit message