如何使用 git format-patch 将提交压缩到一个补丁中?
我在一个分支上有 8 个提交,我想通过电子邮件发送给一些尚未了解 git 的人。 到目前为止,我所做的一切要么给我 8 个补丁文件,要么开始为分支历史记录中的每个提交提供补丁文件,从一开始。 我使用 git rebase --interactive 来压缩提交,但现在我尝试的一切从一开始就给了我无数的补丁。 我究竟做错了什么?
git format-patch master HEAD # yields zillions of patches, even though there's
# only one commit since master
I've got eight commits on a branch that I'd like to email to some people who aren't git enlightened, yet. So far, everything I do either gives me 8 patch files, or starts giving me patch files for every commit in the branch's history, since the beginning of time. I used git rebase --interactive to squash the commits, but now everything I try gives me zillions of patches from the beginning of time. What am I doing wrong?
git format-patch master HEAD # yields zillions of patches, even though there's
# only one commit since master
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我建议在一次性分支上执行此操作,如下所示。 如果您的提交位于“newlines”分支中并且您已经切换回“master”分支,那么这应该可以解决问题:
I'd recommend doing this on a throwaway branch as follows. If your commits are in the "newlines" branch and you have switched back to your "master" branch already, this should do the trick:
只是为了向锅中添加另一种解决方案:
如果你改用这个:
那么它仍然是 8 个补丁...但它们都将在一个补丁文件中,并且将作为一个补丁应用:
Just to add one more solution to the pot:
If you use this instead:
Then it will still be 8 patches... but they'll all be in a single patchfile and will apply as one with:
我总是使用 git diff 所以在你的例子中,类似
I always use git diff so in your example, something like
如您所知,
git format-patch -8 HEAD
将为您提供八个补丁。如果您希望 8 个提交显示为一个,并且不介意重写分支的历史记录 (
ooXABCDEFGH
),您可以:或者,这将是一个更好的解决方案,重播您的所有提交来自
X
的 8 次提交(8 次提交之前的提交)在新分支上这样,您在“delivery”分支上只有一次提交,它代表了您最后 8 次提交
As you already know, a
git format-patch -8 HEAD
will give you eight patches.If you want your 8 commits appear as one, and do not mind rewriting the history of your branch (
o-o-X-A-B-C-D-E-F-G-H
), you could :or, and it would be a better solution, replay all your 8 commits from
X
(the commit before your 8 commits) on a new branchThat way, you only have one commit on the "delivery" branch, and it represent all your last 8 commits
这是 Adam Alexander 答案的改编,以防您的更改发生在主分支中。 执行以下操作:
This is an adaptation of Adam Alexander answer, in case your changes are in master branch. This do the following:
最简单的方法是使用
git diff
,如果您想要 squash 方法输出的组合提交消息,请添加git log
。 例如,要在提交abcd
和1234
之间创建补丁:然后在应用补丁时:
不要忘记
--binary
参数git diff
处理非文本文件(例如图像或视频)时。Easiest way is to use
git diff
, and add ingit log
if you want the combined commit message that the squash method would output. For example, to create the patch between commitabcd
and1234
:Then when applying the patch:
Don't forget the
--binary
argument togit diff
when dealing with non-text files, e.g. images or videos.两个标签之间的格式补丁:
Format-patch between two tags:
基于亚当·亚历山大的回答:
Based on Adam Alexander's answer: