在推送 Git 之前组合多个提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6934752/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:30:01  来源:igfitidea点击:

Combining multiple commits before pushing in Git

gitgit-squash

提问by muudscope

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 rebasedoes this, but I can't make sense of the docs.

我在我的本地存储库中有一堆主题相似的提交。我想在推送到远程之前将它们组合成一个单一的提交。我该怎么做?我认为rebase这样做,但我无法理解文档。

回答by Leopd

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:

你想要做的在 git 中被称为“挤压”。执行此操作时有很多选择(太多?)但如果您只想将所有未推送的提交合并为一个提交,请执行以下操作:

git rebase -i origin/master

This will bring up your text editor (-iis for "interactive") with a file that looks like this:

这将打开您的文本编辑器(-i用于“交互式”),其中包含一个如下所示的文件:

pick 16b5fcc Code in, tests not passing
pick c964dea Getting closer
pick 06cf8ee Something changed
pick 396b4a3 Tests pass
pick 9be7fdb Better comments
pick 7dba9cb All done

Change all the pickto squash(or s) except the first one:

将除第一个之外的所有更改picksquash(或s):

pick 16b5fcc Code in, tests not passing
squash c964dea Getting closer
squash 06cf8ee Something changed
squash 396b4a3 Tests pass
squash 9be7fdb Better comments
squash 7dba9cb All done

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.

瞧!谷歌搜索“git squashing”会给你所有其他可用选项的解释。

回答by Noich

If you have lotsof 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

如果您有很多提交并且只想压缩最后的 X 次提交,请找到要从中开始压缩的提交的提交 ID 并执行

git rebase -i <that_commit_id>

Then proceed as described in leopd's answer, changing all the picks to squashes except the first one.

然后按照 leopd 的回答中的描述继续操作,将除第一个之外的所有picks更改为squashes。

Example:

示例

871adf OK, feature Z is fully implemented      --- newer commit --┐
0c3317 Whoops, not yet...                                         |
87871a I'm ready!                                                 |
643d0e Code cleanup                                               |-- Join these into one
afb581 Fix this and that                                          |
4e9baa Cool implementation                                        |
d94e78 Prepare the workbench for feature Z     -------------------┘
6394dc Feature Y                               --- older commit

You can either do this (write the number of commits):

您可以这样做(写下提交次数):

git rebase --interactive HEAD~[7]

Or this (write the hash of the last commit you don'twant to squash):

或者这(写最后的哈希提交你希望壁球):

git rebase --interactive 6394dc

回答by vikas027

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 pickwith squashin order to remove/merge them into one

这里有很多有效的答案,但我发现这是最简单的。此命令将打开一个编辑器,在那里你可以替换pick使用squash,以消除/它们合并成一个

git rebase -i HEAD~4

where, 4is the number of commits you want to squash into one. This is explained hereas well.

其中,4是您想要压缩为一个的提交数。这也解释here

回答by Justin Weiss

You can do this with git rebase -i, passing in the revision that you want to use as the 'root':

您可以使用git rebase -i,传入您要用作“根”的修订版来执行此操作:

git rebase -i origin/master

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.

将打开一个编辑器窗口,显示您在origin/master. 您可以拒绝提交、将提交压缩为单个提交或编辑以前的提交。

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

http://book.git-scm.com/4_interactive_rebasing.html

and

http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

are the first two good pages I could find.

是我能找到的前两个好页面。

回答by ruediste

I came up with

我想出了

#!/bin/sh

message=`git log --format=%B origin..HEAD | sort | uniq | grep -v '^$'`
git reset --soft origin
git commit -m "$message"

Combines, sorts, unifies and remove empty lines from the commit message. I use this for local changes to a github wiki (using gollum)

从提交消息中组合、排序、统一和删除空行。我使用它对 github wiki 进行本地更改(使用 gollum)

回答by Benny Neugebauer

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:

您可以使用Interactive Rebase 压缩(加入)提交。有一个非常不错的 YouTube 视频,它展示了如何在命令行或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.

如果您已经是 SmartGit 用户,那么您可以选择所有传出提交(通过按住 Ctrl 键)并打开上下文菜单(右键单击)以压缩您的提交。

It's very comfortable:

很舒服:

enter image description here

在此处输入图片说明

There is also a very nice tutorial from Atlassianwhich shows how it works:

还有一个来自Atlassian的非常好的教程,展示了它是如何工作的:

回答by Tomer Ben David

And my way of squashingmultiple pushis (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).

我的squashing多重方式push是(也许你向你自己的分支推送了许多提交,现在你想要做一个拉取请求,你不想用你已经推送的许多提交来混淆它们)。我这样做的方式(据我所知,没有其他更简单的选择)。

  1. Create new branch for the sake of squash(branch from the original branch you wish to pull request to).
  2. Push the newly created branch.
  3. Merge branch with commits (already pushed) to new branch.
  4. Rebase new branch and squash.
  5. Push new branch.
  6. Create new pull request for new branch which now has single commit.
  1. 为了squash(从您希望拉取请求到的原始分支的分支)而创建新分支。
  2. 推送新创建的分支。
  3. 将带有提交(已推送)的分支合并到新分支。
  4. 重新设置新分支和壁球。
  5. 推新分支。
  6. 为现在具有单一提交的新分支创建新的拉取请求。

Example:

例子:

git checkout from_branch_you_wish_to_pull_request_to
git checkout -b new_branch_will_have_single_squashed_commit
git push -u new_branch_will_have_single_squashed_commit
git merge older_branch_with_all_those_multiple_commits
git rebase -i (here you squash)
git push origin new_branch_will_have_single_squashed_commit

You can now pull request into from_branch_you_wish_to_pull_request_to

您现在可以将请求拉入 from_branch_you_wish_to_pull_request_to

回答by Greg Hewgill

You probably want to use Interactive Rebasing, which is described in detail in that link.

您可能想要使用Interactive Rebasing,该链接中对此进行了详细描述。

You can find other good resources if you search for "git rebase interactive".

如果您搜索“git rebase interactive”,您可以找到其他好的资源。