git 如何压缩本地分支上的特定提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24310554/
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 I squash specific commits on a local branch?
提问by Martin
I can find a few answers out there that come close to what I want, but I'm afraid I'm not really experienced enough with Git to exactly understand how to achieve what I want.
我可以在那里找到一些接近我想要的答案,但恐怕我对 Git 的经验不足,无法准确了解如何实现我想要的。
Given a local branch with commits A-B-C-D-E-F-G-H-I-J on it, I want to squash some of the commits together so that I end up with, for example, the equivalent of A-BCD-E-FGH-IJ.
给定一个提交 ABCDEFGHIJ 的本地分支,我想将一些提交压缩在一起,以便我最终得到,例如,相当于 A-BCD-E-FGH-IJ。
Is this possible? The intention is to do this to a local branch, after rebasing, but before merging back into the master branch, in order to create a more concise and informative history.
这可能吗?目的是在变基之后但在合并回主分支之前对本地分支执行此操作,以创建更简洁和信息量更大的历史记录。
回答by cmbuckley
You can do this with rebase
. Assuming commits A–J are on a local branch branchname
built on top of master
, then you can do this:
您可以使用rebase
. 假设提交 A-J 位于branchname
构建在 之上的本地分支上master
,那么您可以执行以下操作:
git checkout branchname
git rebase -i master
You'll be presented with an interactive window like this:
您将看到一个像这样的交互式窗口:
pick A Commit message A
pick B Commit message B
pick C Commit message C
pick D Commit message D
pick E Commit message E
...
Interactive rebase provides instructions for your scenario, so you should change "pick" to "squash" for C and D (and the same for G, H and J):
交互式变基为您的场景提供了说明,因此您应该将 C 和 D 的“pick”更改为“squash”(G、H 和 J 也是如此):
pick A Commit message A
pick B Commit message B
squash C Commit message C
squash D Commit message D
pick E Commit message E
This will squash B, C and D into a single commit, allowing you to change the commit message.
这会将 B、C 和 D 压缩为单个提交,从而允许您更改提交消息。
If you're happy to use the commit message of B, you can use "fixup" instead of "squash" and you won't be prompted to update the commit message.
如果您乐于使用 B 的提交消息,则可以使用“fixup”而不是“squash”,并且不会提示您更新提交消息。
The interactive window is very powerful, and you can reorder commits as much as you want, or even just delete the line (or change the prefix to "drop") and that commit will be removed. For instance, if E fixes a typo that you made in A, you can do this:
交互式窗口非常强大,您可以根据需要对提交重新排序,甚至可以删除该行(或将前缀更改为“drop”),然后该提交将被删除。例如,如果 E 修正了你在 A 中的拼写错误,你可以这样做:
pick A Commit message A
fixup E Commit message E
pick B Commit message B
pick C Commit message C
pick D Commit message D
However, when you reorder commits, you run the risk of having to resolve conflicts along the way. You can always use git rebase --abort
to return to your original state.
但是,当您对提交重新排序时,您将面临不得不在此过程中解决冲突的风险。您可以随时使用git rebase --abort
返回到您的原始状态。