使用 `git show` 创建和应用跨越多个提交的补丁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12868131/
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
Using `git show` to create and apply patches spanning multiple commits
提问by redbmk
Lately I've been using git show <hash>
to create diffs for later reference because it's easier to type than git diff <hash>~ <hash>
and it shows the commit information (timestamp, user, hash, comment). You can then use git apply <filename>
to apply the patch.
最近我一直在使用git show <hash>
创建差异以供以后参考,因为它比输入更容易,git diff <hash>~ <hash>
并且它显示了提交信息(时间戳、用户、哈希、评论)。然后您可以使用git apply <filename>
来应用补丁。
I discovered that git show -3
will show the last three commits along with the same extra information. However, git apply
will squash it all into the working directory as unstaged changes, and loses all the commit information.
我发现这git show -3
将显示最后三个提交以及相同的额外信息。但是,git apply
会将其作为未暂存的更改全部压缩到工作目录中,并丢失所有提交信息。
Is there something in git that will apply all that information? It would be a lot simpler to just pass in a flag than breaking the patch up into three files, applying them severally, and creating new commits.
git 中有什么东西可以应用所有这些信息吗?与将补丁分成三个文件,分别应用它们并创建新提交相比,只传递一个标志要简单得多。
采纳答案by Lily Ballard
You can use git format-patch
to generate MIME emails representing the commits, including their metadata (message, authorship, etc). You can then reapply these with git am
.
您可以使用git format-patch
生成表示提交的 MIME 电子邮件,包括它们的元数据(消息、作者等)。然后,您可以使用 重新应用这些git am
。
So git format-patch HEAD~3
will generate 3 patches for the last 3 commits, and you can then pipe these all into git am
. If you want to be simpler, git format-patch --stdout HEAD~3
will send the MIME messages out on stdout, so you can pipe them around where you want instead of dealing with 3 separate files.
Sogit format-patch HEAD~3
将为最后 3 个提交生成 3 个补丁,然后您可以将它们全部导入git am
. 如果你想更简单,git format-patch --stdout HEAD~3
将在标准输出上发送 MIME 消息,这样你就可以将它们传送到你想要的地方,而不是处理 3 个单独的文件。
Of course, if you're trying to save commits to reference later, why not just tag them? You can then reapply the commits from them using git cherry-pick
.
当然,如果您想保存提交以供以后参考,为什么不直接标记它们呢?然后,您可以使用git cherry-pick
.
回答by Campa
In Linux you can pipe your delta-generator--- git-show
or git-diff
, for instance --- with the git-apply
command.
在 Linux 中,您可以通过管道连接您的delta-generator---git-show
或者git-diff
,例如 --- 使用git-apply
命令。
(It is also probable safer to always enable 3-way merge with the -3
option)
(始终使用该-3
选项启用 3 路合并也可能更安全)
> git show <sha1> [<path>] | git apply -3
> git diff <sha1-a> <sha1-b> [<path>] | git apply -3
Examples:
例子:
- apply the edits on a specific file made in abcdef:
- 在abcdef 中对特定文件应用编辑:
> git show abcdef dir/file.c | git apply -3
- apply difference on a file of two different commits abcdefand 123456
- 在两个不同提交abcdef和123456的文件上应用差异
> git diff abcdef 123456 foo.h | git apply -3
> git diff abcdef 123456 foo.h | git apply -3
回答by Adam Dymitruk
try git bundle
for sending specific parts of history.
尝试git bundle
发送历史的特定部分。