Brainstorm's snippets (1/247)

  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])

Explanation: