我怎样才能将两个提交合并成一个?

我试图合并2个提交到1,所以我遵循“压扁与rebase提交”从git准备 。

我跑了

git rebase --interactive HEAD~2 

在最终的编辑器中,我改变pick squash ,然后保存退出,但重build失败,错误

如果没有以前的提交,就不能“挤压”

现在我的工作树已经达到了这个状态,我正在恢复。 命令git rebase --interactive HEAD~2失败

交互式重新分配已经开始

git rebase --continue失败

如果没有以前的提交,就不能“挤压”

回到你开始的地方

 $ git rebase --abort 

说你的历史是

 $ git log --pretty=oneline a931ac7c808e2471b22b5bd20f0cad046b1c5d0d c b76d157d507e819d7511132bdb5a80dd421d854f b df239176e1a2ffac927d8b496ea00d5488481db5 a 

也就是说,a是第一次提交,然后是b,最后是c。 提交c之后,我们决定一起压缩b和c:

运行git rebase --interactive HEAD~2给你一个编辑器

 pick b76d157 b pick a931ac7 c # Rebase df23917..a931ac7 onto df23917 # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. # 

改变b的picksquash会导致你看到的错误,但是如果你想把c压缩到b(新进入老的),把文本改成

 pick b76d157 b squash a931ac7 c 

并保存退出你的编辑器,你会得到另一个编辑器的内容是

 # This is a combination of 2 commits. # The first commit's message is: b # This is the 2nd commit message: c 

当您保存并退出时,编辑文件的内容将成为新组合提交的提交消息:

 $ git log --pretty=oneline 18fd73d3ce748f2a58d1b566c03dd9dafe0b6b4f b and c df239176e1a2ffac927d8b496ea00d5488481db5 a 

如果有多个提交,你可以使用git rebase -i将两个提交压缩成一个。

如果只有两个要合并的提交,并且是“最近的两个”,则可以使用以下命令将两个提交合并为一个:

 git reset --soft "HEAD^" git commit --amend 

首先,你应该检查你有多less提交:

 git log 

有两种状态:

一个是只有两个提交:

例如:

 commit A commit B 

(在这种情况下,你不能使用git rebase来做)你需要做下面的事情。

 $ git reset --soft HEAD^1 $ git commit --amend 

另一个是有两个以上的提交; 你想合并提交C和D.

例如:

 commit A commit B commit C commit D 

(在这种情况下,你可以使用git rebase)

 git rebase -i B 

而不是用“南瓜”来做。 其余的很简单。 如果您还不知道,请阅读http://zerodie.github.io/blog/2012/01/19/git-rebase-i/

假设你在自己的主题分支。 如果你想把最后2个提交合并成一个,看起来像个英雄,那么在你提交最后两个提交之前,分支提交。

 git checkout -b temp_branch HEAD^2 

然后在这个新的分支中压扁另一个分支:

 git merge branch_with_two_commits --squash 

这将带来变化,但不承诺。 所以只要提交他们,你就完成了。

 git commit -m "my message" 

现在你可以把这个新的主题分支合并回你的主分支。

你可以取消rebase与

 git rebase --abort 

而且当你再次运行交互式的rebase命令时,“squash; 提交必须低于列表中的提交提交

我经常使用git reset –在你想合并的多个提交之前复制一个基本版本,然后我做一个新的提交,这样可以让你提交最新的,确保你的版本是HEAD之后,你推到服务器。

 commit ac72a4308ba70cc42aace47509a5e Author: <me@me.com> Date: Tue Jun 11 10:23:07 2013 +0500 Added algorithms for Cosine-similarity commit 77df2a40e53136c7a2d58fd847372 Author: <me@me.com> Date: Tue Jun 11 13:02:14 2013 -0700 Set stage for similar objects commit 249cf9392da197573a17c8426c282 Author: Ralph <ralph@me.com> Date: Thu Jun 13 16:44:12 2013 -0700 Fixed a bug in space world automation 

如果我想合并头两个提交到一个,首先我使用:

 git reset --mixed 249cf9392da197573a17c8426c282 

“249cf9392da197573a17c8426c282”是第三个版本,也是你合并之前的基础版本,之后,我做了一个新的提交:

 git add . git commit -m 'some commit message' 

这是所有,希望是每个人的另一种方式。

仅供参考,从git reset --help

  --mixed Resets the index but not the working tree (ie, the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action. 

Rebase:你不需要它:

对于最常见的情况,一个更简单的方法。

在多数情况下:

其实如果你只想简单地将几个提交合并成一个 ,而不需要dropreword和其他的rebase工作。

你可以简单地做:

 git reset --soft "HEAD~n" 
  • 假设〜n是软提交的数量(即~1~2 ,…)

然后,使用以下命令修改提交消息。

 git commit --amend 

这与一个长距离的squash和一个pick很相似。

它适用于n提交但不只是两个提交上面的答案提示。

$ git rebase --abort

如果你想撤销git rebase,可以随时运行这段代码

$ git rebase -i HEAD~2

重新申请最后两个提交。 上面的命令将打开一个代码编辑器

  • [ 最新的提交将在底部 ]。 将上次提交更改为压扁(s)。 由于南瓜将融合与以前的承诺。
  • 然后按esc键并input:wq保存并closures

之后:wq你将会进入激活的rebase模式

注意 :如果没有警告/错误消息,你会得到另一个编辑器,如果有错误或警告另一个编辑器不会显示,你可以通过运行$ git rebase --abort中止,如果你看到一个错误或警告其他只是继续运行$ git rebase --continue

你会看到你的2提交消息。 select一个或写你自己的提交信息,保存并退出[:wq]

注2:如果运行rebase命令,您可能需要强制将更改推送到远程回购站

$ git push -f

$ git push -f origin master

因为我使用git cherry-pick来处理所有事情,所以即使在这里也是自然而然的。

考虑到我已经检出了branchX ,并且在它的顶端有两个提交,其中我想创build一个合并其内容的提交,我这样做:

 git checkout HEAD^ // Checkout the privious commit git cherry-pick --no-commit branchX // Cherry pick the content of the second commit git commit --amend // Create a new commit with their combined content 

如果我想更新branchX (我想这是这个方法的branchX ),我也必须:

 git checkout branchX git reset --hard <the_new_commit> 

如果你的主分支git log看起来如下所示:

 commit ac72a4308ba70cc42aace47509a5e Author: <me@me.com> Date: Tue Jun 11 10:23:07 2013 +0500 Added algorithms for Cosine-similarity commit 77df2a40e53136c7a2d58fd847372 Author: <me@me.com> Date: Tue Jun 11 13:02:14 2013 -0700 Set stage for similar objects commit 249cf9392da197573a17c8426c282 Author: Ralph <ralph@me.com> Date: Thu Jun 13 16:44:12 2013 -0700 Fixed a bug in space world automation 

而您想要合并前两个提交,只需执行以下简单步骤:

  1. 首先要安全地检查第二个最后提交在一个单独的分支。 你可以给分支命名。 git checkout 77df2a40e53136c7a2d58fd847372 -b merged-commits
  2. 现在,只需从上次提交到新分支中挑选您的更改: git cherry-pick -n -x ac72a4308ba70cc42aace47509a5e 。 (解决冲突,如果出现任何)
  3. 所以现在,你最后一次提交的更改在你的第二次提交中。 但是你仍然必须提交,所以首先添加你刚刚挑选的更改,然后执行git commit --amend

而已。 如果你愿意,你可以在分支“合并提交”中推送这个合并版本。

另外,您现在可以在主分支中丢弃背对背的两个提交。 只需更新您的主分支:

 git checkout master git reset --hard origin/master (CAUTION: This command will remove any local changes to your master branch) git pull