git 如何将两个提交合并为一个提交?

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

How can I combine two commits into one commit?

gitmergebranchgit-svnrebase

提问by Don P

I have a branch 'firstproject' with 2 commits. I want to get rid of these commits and make them appear as a single commit.

我有一个分支 'firstproject' 有 2 个提交。我想摆脱这些提交并使它们显示为单个提交。

The command git merge --squashsounds promising, but when I run git merge --squashmy terminal just brings up options for the command. What is the correct command?

该命令git merge --squash听起来很有希望,但是当我运行git merge --squash终端时,只会显示该命令的选项。什么是正确的命令?

Commit 1:
Added 'homepage.html'
Added 'contacts.html'

Commit 2:
Added 'homepage.php'
Added 'homepage.php'
Deleted 'homepage.html'
Deleted 'contacts.html'

回答by meagar

You want to git rebase -ito perform an interactive rebase.

您想要git rebase -i执行交互式 rebase

If you're currently onyour "commit 1", and the commit you want to merge, "commit 2", is the previous commit, you can run git rebase -i HEAD~2and change the first word of the second line from "pick" to "squash". Then write your file, and quit. Git will squash your first commit into your second last commit.

如果您当前“提交 1”上,并且要合并的提交“提交 2”是前一个提交,则可以运行git rebase -i HEAD~2并将第二行的第一个单词从“pick”更改为“squash” . 然后写你的文件,然后退出。Git 会将您的第一次提交压缩到您的最后一次提交中。

Note that this process rewrites the history of your branch. If you are pushing your code somewhere, you'll have to git push -fand anybody sharing your code will have to jump through some hoops to pull your changes.

请注意,此过程会重写分支的历史记录。如果你把你的代码推到某个地方,你就必须这样做git push -f,任何共享你代码的人都必须跳过一些障碍来拉动你的更改。

Note that if the two commits in question aren'tthe last two commits on the branch, the process will be slightly different.

请注意,如果有问题的两次提交不是分支上的最后两次提交,则过程将略有不同。

回答by Stachu

Lazy simple version for forgetfuls like me:

懒惰的简单版本,适合像我这样健忘的人:

git rebase -i HEAD~3or however many commits instead of 3.

git rebase -i HEAD~3或者无论提交多少次而不是 3 次。

Turn this

转这个

pick YourCommitMessageWhatever
pick YouGetThePoint
pick IdkManItsACommitMessage

into this

进入这个

pick YourCommitMessageWhatever
s YouGetThePoint
s IdkManItsACommitMessage

and do some action where you hit escthen enterto save the changes. [1]

和做一些动作,你打esc,然后enter保存更改。[1]

When the next screen comes up, get rid of those garbage # lines [2] and create a new commit message or something, and do the same escapeenteraction. [1]

当下一个屏幕出现时,去掉那些垃圾 # 行 [2] 并创建一个新的提交消息或其他内容,然后执行相同的escapeenter操作。[1]

Wowee, you have fewer commits. Or you just broke everything.

Wowee,你的提交更少了。或者你只是打破了一切。



[1] - or whatever works with your git configuration. This is just a sequence that's efficient given my setup.

[1] - 或任何适用于您的 git 配置的东西。鉴于我的设置,这只是一个有效的序列。

[2] - you'll see some stuff like # this is your n'th commita few times, with your original commits right below these message. You want to remove these lines, and create a commit message to reflect the intentions of the n commits that you're combining into 1.

[2] - 你会看到一些东西,比如# this is your n'th commit你的原始提交就在这些消息下面。您想删除这些行,并创建一条提交消息以反映您正在合并为 1 的 n 次提交的意图。

回答by user5820972

  1. Checkout your branch and count quantity of all your commits.
  2. Open git bash and write: git rebase -i HEAD~<quantity of your commits>(i.e. git rebase -i HEAD~5)
  3. In opened txtfile change pickkeyword to squashfor all commits, except first commit (which is on the top). For top one change it to reword(which means you will provide a new comment for this commit in the next step) and click SAVE! If in vim, press escthen save by entering wq!and press enter.
  4. Provide Comment.
  5. Open Git and make "Fetch all" to see new changes.
  1. 检查您的分支并计算所有提交的数量。
  2. 打开 git bash 并写入:(git rebase -i HEAD~<quantity of your commits>git rebase -i HEAD~5
  3. 在打开的txt文件中更改pick关键字 tosquash用于所有提交,除了第一次提交(位于顶部)。对于顶部的将其更改为reword(这意味着您将在下一步中为此提交提供新评论)并单击保存!如果在vim中,按esc然后输入保存wq!并按回车。
  4. 提供评论。
  5. 打开 Git 并执行“Fetch all”以查看新更改。

Done

完毕