在 Git 上将一组提交合并为一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6884022/
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
Collapsing a Group of Commits into One on Git
提问by Rodrogo
I have the habit of making a huge number of small commits, and I'm fine with it. But I would like to, from time to time, take a bunch of those linear commits and collapse them together as just one commit with the ability to write a new commit message.
我有进行大量小提交的习惯,我对此很好。但我想不时地将一堆这些线性提交合并在一起,作为一个能够编写新提交消息的提交。
I've looked into the documentation but seemed a little to cryptic to me. Does anybody knows how to do that?
我查看了文档,但对我来说似乎有点神秘。有人知道怎么做吗?
采纳答案by yfeldblum
Suppose you want to rewrite the history of the tree going back until (but not including) commit a739b0d
.
假设您要重写树的历史记录,直到(但不包括) commit a739b0d
。
export EDITOR=vim # or your favorite editor
git rebase a739b0d --interactive
Be sure to read up on interactive rebasingfirst.
请务必先阅读交互式变基。
回答by Ben Amos
Assuming you don't care about retaining any of your existing commit messages, there's a nifty (and fast) git recipe you can use. First, make sure your branch is checked out:
假设您不关心保留任何现有的提交消息,那么您可以使用一个漂亮(且快速)的 git 配方。首先,确保您的分支已签出:
git checkout <branch-to-squash>
For safety, lets tag the current commit.
为了安全起见,让我们标记当前的提交。
git tag my-branch-backup
Next, move the branch HEAD back to your last good commit (without modifying the workspace or index). EDIT: The last good commit is the most recent commit on your branch that you want to retain.
接下来,将分支 HEAD 移回上次好的提交(不修改工作区或索引)。编辑:最后一次好的提交是您要保留的分支上的最新提交。
git reset --soft <last-good-commit>
Using git status
, you'll notice that all changes on your feature branch are now staged. All that's left to do is ...
使用git status
,您会注意到功能分支上的所有更改现在都已暂存。剩下要做的就是......
git commit
This method is great for consolidating long, convoluted git histories and gnarly merges. Plus, there's no merge/rebase conflicts to resolve!
这种方法非常适合整合冗长、复杂的 git 历史和粗糙的合并。另外,没有要解决的合并/变基冲突!
Now, if you need to retain any of your existing commit messages or do anything fancier than the above allows, you'll want to use git rebase --interactive
.
现在,如果您需要保留任何现有的提交消息或做任何比上述允许的更有趣的事情,您将需要使用git rebase --interactive
.
Solution derived from: http://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit
解决方案来自:http: //makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit
Reference: http://git-scm.com/docs/git-reset
参考:http: //git-scm.com/docs/git-reset
Reference: http://git-scm.com/docs/git-rebase
回答by kevmo
Use the command git rebase -i <commit>
where <commit>
is the SHA for the last stable commit.
使用命令git rebase -i <commit>
where <commit>
is the SHA for the last stable commit。
This will take you to your editor where you can replace the label pick
that is next to each commit since the <commit>
you included as an argument to your interactive rebase command. On the command you want to start collapsing at, replace pick
with reword
, and for each commit thereafter which you wish to collapse into it, replace pick
with fixup
. Save, and you'll then be allowed to provide a new commit message.
这将带您到您的编辑器,您可以在其中替换pick
每个提交旁边的标签,因为<commit>
您将其作为参数包含在交互式 rebase 命令中。在您想要开始折叠的命令上,替换pick
为reword
,然后对于您希望折叠到其中的每个提交,替换pick
为fixup
。保存,然后您将被允许提供新的提交消息。
回答by Matt Ball
You can squash any number of commits into a single one using
您可以使用以下方法将任意数量的提交压缩为一个
git rebase --interactive <commit>