Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to cherry-pick a commit from another git repository?

I'm working with a git repository that needs a commit from another git repository that knows nothing of the first.

Typically I would cherry-pick using the HEAD@{x} in the reflog, but because this .git knows nothing of this reflog entry (different physical directory), how can I cherry-pick this, or can I?

I'm using git-svn. My first branch is using git-svn of the trunk of a Subversion repo, and the next branch is using git-svn on a Subversion branch.

like image 911
gitcoder182 Avatar asked Feb 25 '11 16:02

gitcoder182


People also ask

Can I cherry pick a commit from another repo?

It is possible to cherry pick from another repo using the command line. You will first need to add the other repository as a remote and then fetch the changes. From there, you should be able to see the commit in your repo and cherry pick it.


2 Answers

You'll need to add the other repository as a remote, then fetch its changes. From there you see the commit and you can cherry-pick it.

Like that:

git remote add other https://example.link/repository.git git fetch other 

Now you have all the information to simply do git cherry-pick.

When done, you may want to remove the remote again, if you don't need it any more, with

git remote remove other 

More info about working with remotes here: https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

like image 32
CharlesB Avatar answered Sep 22 '22 19:09

CharlesB


The answer, as given, is to use format-patch but since the question was how to cherry-pick from another folder, here is a piece of code to do just that:

$ git --git-dir=../<some_other_repo>/.git \ format-patch -k -1 --stdout <commit SHA> | \ git am -3 -k 

Explanation from Cong Ma comment Aug 28 '14

git format-patch command creates a patch from some_other_repo's commit specified by its SHA (-1 for one single commit alone). 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).

like image 112
Robert Wahler Avatar answered Sep 21 '22 19:09

Robert Wahler