在推送 Git 之前合并多个提交
我的本地存储库上有一堆主题相似的提交。我想在推送到远程之前将它们组合成一个提交。我该怎么做?我认为 rebase
可以做到这一点,但我无法理解这些文档。
I have a bunch of commits on my local repository which are thematically similar. I'd like to combine them into a single commit before pushing up to a remote. How do I do it? I think rebase
does this, but I can't make sense of the docs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你想要做的事情在 git 中被称为“挤压”。执行此操作时有很多选项(太多?),但如果您只想将所有未推送的提交合并到单个提交中,请执行以下操作:
这将打开您的文本编辑器(
-i< /code> 代表“交互”),文件如下所示:
将所有
pick
更改为squash
(或s
),除了第一个:保存文件并退出编辑器。然后,另一个文本编辑器将打开,让您将所有提交中的提交消息合并为一个大提交消息。
瞧!谷歌搜索“git scraping”将为您提供所有其他可用选项的解释。
What you want to do is referred to as "squashing" in git. There are lots of options when you're doing this (too many?) but if you just want to merge all of your unpushed commits into a single commit, do this:
This will bring up your text editor (
-i
is for "interactive") with a file that looks like this:Change all the
pick
tosquash
(ors
) except the first one:Save your file and exit your editor. Then another text editor will open to let you combine the commit messages from all of the commits into one big commit message.
Voila! Googling "git squashing" will give you explanations of all the other options available.
如果您有很多提交,并且只想压缩最后 X 个提交,请找到要开始压缩的提交的提交 ID,
然后执行 leopd 的回答中所述的操作,更改所有除了第一个之外,
pick
会squash
es。示例:
您可以执行此操作(写入提交数):
或者这样(写下您不想想要压缩的最后一次提交的哈希值):
If you have lots of commits and you only want to squash the last X commits, find the commit ID of the commit from which you want to start squashing and do
Then proceed as described in leopd's answer, changing all the
pick
s tosquash
es except the first one.Example:
You can either do this (write the number of commits):
Or this (write the hash of the last commit you don't want to squash):
这里有很多可行的答案,但我发现这是最简单的。此命令将打开一个编辑器,您可以在其中将
pick
替换为squash
,以便将它们删除/合并到一个编辑器中,其中
4
是您想要压缩为一次的提交数量。 此处也对此进行了解释。如果您希望
压缩
提交(其中还包括第一个提交),请使用此命令There are quite a few working answers here, but I found this the easiest. This command will open up an editor, where you can just replace
pick
withsquash
in order to remove/merge them into onewhere,
4
is the number of commits you want to squash into one. This is explained here as well.If you are looking to
squash
the commits which also includes the very first commit, then use this command您可以使用 git rebase -i 来执行此操作,传入要用作“根”的修订版:
将打开一个编辑器窗口,显示您在上次提交后所做的所有提交
起源/主
。您可以拒绝提交、将提交压缩为单个提交或编辑以前的提交。有一些资源可能可以更好地解释这一点,并显示一些其他示例:
http://book.git -scm.com/4_interactive_rebasing.html
和
http://gitready.com/advanced/2009/02/10/squashing-commits- with-rebase.html
是我能找到的前两个好页面。
You can do this with
git rebase -i
, passing in the revision that you want to use as the 'root':will open an editor window showing all of the commits you have made after the last commit in
origin/master
. You can reject commits, squash commits into a single commit, or edit previous commits.There are a few resources that can probably explain this in a better way, and show some other examples:
http://book.git-scm.com/4_interactive_rebasing.html
and
http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html
are the first two good pages I could find.
我想出了
合并、排序、统一和删除提交消息中的空行。我用它来对 github wiki 进行本地更改(使用 gollum)
I came up with
Combines, sorts, unifies and remove empty lines from the commit message. I use this for local changes to a github wiki (using gollum)
您可以使用交互式变基压缩(加入)提交。有一个非常漂亮的 YouTube 视频,展示了如何在命令行或使用 SmartGit 执行此操作:
如果您已经是然后,SmartGit 用户可以选择所有传出提交(通过按住 Ctrl 键)并打开上下文菜单(右键单击)来压缩您的提交。
非常舒服:
Atlassian 还提供了一个非常好的教程,其中展示了如何作品:
You can squash (join) commits with an Interactive Rebase. There is a pretty nice YouTube video which shows how to do this on the command line or with SmartGit:
If you are already a SmartGit user then you can select all your outgoing commits (by holding down the Ctrl key) and open the context menu (right click) to squash your commits.
It's very comfortable:
There is also a very nice tutorial from Atlassian which shows how it works:
我
压缩
多个push
的方式是(也许您将许多提交推送到自己的分支,现在您希望执行拉取请求,并且不想让它们混乱有许多您已经推送的提交)。我这样做的方式(据我所知,没有其他更简单的选择)。squash
创建新分支(来自您希望拉取请求的原始分支的分支)。示例:
您现在可以将请求拉入
from_branch_you_wish_to_pull_request_to
And my way of
squashing
multiplepush
is (perhaps you pushed to your own branch many commits and now you wish to do a pull request and you don't want to clutter them with many commits which you have already pushed). The way I do that (no other simpler option as far as I can tell is).squash
(branch from the original branch you wish to pull request to).Example:
You can now pull request into
from_branch_you_wish_to_pull_request_to
您可能想使用 交互式变基,该链接中有详细描述。
如果搜索“git rebase Interactive”,您可以找到其他好的资源。
You probably want to use Interactive Rebasing, which is described in detail in that link.
You can find other good resources if you search for "git rebase interactive".