git:将一个 repo 中的 commit 引入的更改应用到另一个 repo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3816040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:10:42  来源:igfitidea点击:

git: Apply changes introduced by commit in one repo to another repo

git

提问by takeshin

I have a repo1and repo2on local machine. They are very similar, but the latter is some kind of other branch (repo1is not maintained anymore).

我在本地机器上有一个repo1repo2。它们非常相似,但后者是某种其他分支(repo1不再维护)。

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

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

如何申请通过提交所做的更改<some_sha>repo1repo2

Do I need to prepare some patch, or is it possible to do some cherry-pickbetween the repos?

我需要准备一些补丁,还是可以cherry-pick在 repos 之间做一些?

How about doing the same but for range of commits?

除了提交范围之外,如何做同样的事情?

采纳答案by Jakub Nar?bski

As a hack, you can try modifying recipe for comparing commits in two different repositories on GitTips page, i.e.:

作为一个黑客,您可以尝试修改比较 GitTips 页面上两个不同存储库中提交的配方,即:

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

where ../repois path to the other repository.

../repo另一个存储库的路径在哪里。

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

使用现代 Git,您可以使用cherry-pick使用多个修订和修订范围。

The $(git --git-dir=../repo/.git rev-parse --verify <commit>)is here to translate <commit>(for example HEAD, or v0.2, or master~2, which are values in the second repository you copy from) into SHA-1 identifier of commit. If you know SHA-1 of a change you want to pick, it is not necessary.

$(git --git-dir=../repo/.git rev-parse --verify <commit>)是在这里翻译<commit>(例如HEAD,或v0.2,或master~2,其中是在从复制第二存储库值)转换成提交的SHA-1的标识符。如果您知道要选择的更改的 SHA-1,则没有必要。

NOTEhowever that Git can skip copying objects from source repository, as it doesn't know that the alternate object repository is only temporary, for one operation. You might need to copy objects from the second repository with:

但是请注意,Git 可以跳过从源存储库复制对象,因为它不知道备用对象存储库只是临时的,对于一个操作。您可能需要使用以下命令从第二个存储库复制对象:

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

This puts those objects borrowed from second repository in original repository storage

这会将从第二个存储库借用的对象放入原始存储库存储中

Not tested.

未测试。



A not so hacky solution is to follow knittl answer:

一个不那么hacky的解决方案是遵循knittl answer

  • Go to second repository you want to copy commits from, and generate patches from commits you want with git format-patch
  • Optionally, copy patches (0001-* etc.) to your repository
  • Use git am --3wayto apply patches
  • 转到您想要从中复制提交的第二个存储库,并从您想要的提交中生成补丁 git format-patch
  • 或者,将补丁(0001-* 等)复制到您的存储库
  • 使用git am --3way应用补丁

回答by knittl

You probably want to use git format-patchand then git amto apply that patch to your repository.

您可能想要使用git format-patch然后git am将该补丁应用到您的存储库。

/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

回答by wRAR

You can do cherry-pickif you add the second repo as a remote to the first (and then fetch).

cherry-pick如果将第二个 repo 作为远程添加到第一个(然后是fetch),则可以执行此操作。