您如何获取 git diff 文件,并将其应用到作为同一存储库副本的本地分支?

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

How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?

gitdiffgit-diff

提问by Mike_K

I have a .diff file created by a coworker, and would like to apply the changes listed in that diff file to my local branch of the exact same repository. I do not have access to that worker's pc or branch that was used to generate this diff file.

我有一个同事创建的 .diff 文件,并且想将该 diff 文件中列出的更改应用到我的完全相同存储库的本地分支。我无权访问用于生成此差异文件的该工人的 pc 或分支。

Obviously I could go line by line and retype everything, but i'd rather not subject the system to human error. What's the easiest way to do this?

显然我可以一行一行地重新输入所有内容,但我不想让系统受到人为错误的影响。什么是最简单的方法来做到这一点?

回答by Philipp

Copy the diff file to the root of your repository, and then do:

将 diff 文件复制到存储库的根目录,然后执行以下操作:

git apply yourcoworkers.diff

More information about the applycommand is available on its man page.

有关该apply命令的更多信息,请参见其手册页

By the way: A better way to exchange whole commits by file is the combination of the commands git format-patchon the sender and then git amon the receiver, because it also transfers the authorship info and the commit message.

顺便说一句:按文件交换整个提交的更好方法是在git format-patch发送方和git am接收方上组合命令,因为它还传输作者信息和提交消息。

If the patch application fails and if the commits the diff was generated from are actually in your repo, you can use the -3option of applythat tries to merge in the changes.

如果补丁应用程序失败并且生成差异的提交实际上在您的存储库中,您可以使用尝试合并更改的-3选项apply

It also works with Unix pipe as follows:

它也适用于 Unix 管道,如下所示:

git diff d892531 815a3b5 | git apply