从 git 存储库创建补丁或差异文件并将其应用到另一个不同的 git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28192623/
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-09 02:51:49  来源:igfitidea点击:

Create patch or diff file from git repository and apply it to another different git repository

gitdiffpatchgit-diffgit-apply

提问by zatamine

I work on WordPress based project and I want to patch my project at each new release version of WP. For this, I want generate a patch between two commits or tags.

我从事基于 WordPress 的项目,我想在 WP 的每个新发布版本上修补我的项目。为此,我想在两个提交或标签之间生成一个补丁。

For example, in my repo /www/WPI do this :

例如,在我的回购中,/www/WP我这样做:

$git patch-format com1..com2 --stdout > ~/patchs/mypatch.patch

$git patch-format com1..com2 --stdout > ~/patchs/mypatch.patch

Or

或者

$git patch-format tag1..tag2 --stdout > ~/patchs/mypatch.patch

$git patch-format tag1..tag2 --stdout > ~/patchs/mypatch.patch

/www/WPgit natif WordPress

/www/WPgit natif WordPress

/www/myprojectMy git project WordPress based

/www/myproject我的 git 项目基于 WordPress

The git applycommand line doesn't work, I think because we are in different repositories.

git apply命令行是不行的,我想是因为我们是在不同的存储库。

Can I generate a patch file without a commit, just a differential and apply it to another git repository ?

我可以生成一个没有提交的补丁文件,只是一个差异并将其应用到另一个 git 存储库吗?

Thanks advance.

先谢谢了。

回答by Enrico Campidoglio

You can just use git diffto produce a unified diffsuitable for git apply:

您可以使用git diff来生成适合于的统一差异git apply

git diff tag1..tag2 > mypatch.patch

You can then apply the resulting patch with:

然后,您可以应用生成的补丁:

git apply mypatch.patch

回答by kenorb

To produce patch for several commits, you should use format-patchgit command, e.g.

要为多个提交生成补丁,您应该使用format-patchgit 命令,例如

git format-patch -k --stdout R1..R2

This will export your commits into patch file in mailbox format.

这会将您的提交以邮箱格式导出到补丁文件中。

To generate patch for the last commit, run:

要为最后一次提交生成补丁,请运行:

git format-patch -k --stdout HEAD^

Then in another repository apply the patch by amgit command, e.g.

然后在另一个存储库中通过amgit 命令应用补丁,例如

git am -3 -k file.patch

See: man git-format-patchand git-am.

见:man git-format-patchgit-am