Git: apply the changes made by commit in one repo to another repo

I have repo1 and repo2 on the local machine. They are very similar, but the latter is a kind of different branch ( repo1 no longer supported).

 /path/to/repo1 $ git log HEAD~5..HEAD~4 <some_sha> Add: Introduce feature X 

How to apply the changes made by commit <some_sha> in repo1 to repo2 ?

Do I need to prepare any patch or can I make some cherry-pick between repositories?

How to do the same, but for a range of commits?

+74
git
Sep 28 '10 at 18:49
source share
4 answers

As a hack, you can try changing the recipe for comparing commits in two different repositories on the GitTips page , that is:

 GIT_ALTERNATE_OBJECT_DIRECTORIES=../repo/.git/objects \ git cherry-pick $(git --git-dir=../repo/.git rev-parse --verify <commit>) 

where ../repo is the path to another repository.

With modern Git, you can use multiple versions and revision ranges with cherry-pick .

$(git --git-dir=../repo/.git rev-parse --verify <commit>) here to translate <commit> (e.g. HEAD or v0.2 , or master~2 ), which are values ​​in the second repository from which you will copy) to the SHA-1 identifier for commit. If you know the SHA-1 change you want to select, this is not necessary.

NOTE, however, that Git may skip copying objects from the source repository, because it does not know that the alternative object repository is temporary for only one operation. You may need to copy objects from the second repository using:

 GIT_ALTERNATE_OBJECT_DIRECTORIES=../repo/.git/objects git repack -a -d -f 

This puts those objects that are borrowed from the second store into the store of the original store.

Not tested.




Not so hacky solution should follow the knittl answer :

  • Go to the second repository from which you want to copy the commit, and create the fixes from the commits you want using git format-patch
  • If necessary, copy the fixes (0001- *, etc.) to your repository.
  • Use git am --3way to apply the fixes.
+20
Sep 28 2018-10-10T00:
source share

You might want to use git format-patch and then git am to apply this patch to your repository.

 /path/to/1 $ git format-patch sha1^..sha1 /path/to/1 $ cd /path/to/2 /path/to/2 $ git am -3 /path/to/1/0001-…-….patch 

Or in one line:

 /path/to/2 $ git --git-dir=/path/to/1/.git format-patch --stdout sha1^..sha1 | git am -3 
+143
Sep 28 '10 at 7:23
source share

You can do cherry-pick adding the second repo as remote to the first (and then fetch ).

+59
Sep 28 '10 at 19:29
source share
+4
Feb 16 '12 at 23:45
source share



All Articles