您如何获取 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
How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?
提问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 apply
command 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-patch
on the sender and then git am
on 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 -3
option of apply
that tries to merge in the changes.
如果补丁应用程序失败并且生成差异的提交实际上在您的存储库中,您可以使用尝试合并更改的-3
选项apply
。
It also works with Unix pipe as follows:
它也适用于 Unix 管道,如下所示:
git diff d892531 815a3b5 | git apply