0

With reference to this thread: Is it possible to cherry-pick a commit from another git repository?

.. and this answer provided in there (which is what I need):

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

Is there a possible variation of this which can automatically also record (add to the commit messages?) the relevant git hashes from where I'm patching from to where I'm applying to?

Manually editing is possible but very tedious for +100 commits ...

v01d
  • 189
  • 1
  • 12

1 Answers1

3
git fetch <path_to_some_other_repo> <ref_that_has_the_commit>
git cherry-pick <the_commit> -x

-x appends a line that says "(cherry picked from commit …​)" to the original commit message. But it doesn't record which repository the commit is from.

Update:

The commits you want to pick are reachable from one or more branches or tags. Branches and tags belong to refs. In most cases you can't fetch a random single commit, unless the remote repository allows it. In order to get the commits into your current repository, you need to fetch the refs from the other repository. Suppose the commits are from the branch foo, the history is A-B-C-D-E-F-G and you want to pick C, D, and E. You need to run git fetch origin foo before you cherry-pick these commits to the current branch. You can specify the commits in git cherry-pick, either one by one or in a range by X..Y or maybe X...Y:

git cherry-pick C D E -x
git cherry-pick B..E -x

The graph of foo may be more complicated than this simple example, but you can always find a proper way to specify them all.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Hey, couple of questions: what is that , not the sha1 of the commit? Currently I want to do this on 106 commits from this other repo onto my destination repo. Is there way to specify the range or total number of commits to cherry pick? With the format-patch I used -k 106 starting from top commit sha. – v01d Jul 17 '18 at 02:25
  • thank you, great answer. Now I'm looking at this, tried it, and I don't understand when would one prefer (or have no option but ..) to do the patching as I originally referred to in my question, rather then going the fetch-cherry-pick way ? – v01d Jul 17 '18 at 03:37
  • 1
    @v01d Patches take fewer network and local storage costs in most cases. And it's easier to deliver patches than git meta data when there is no network. – ElpieKay Aug 04 '18 at 03:49