我试图合并2个提交为1,所以我遵循“压缩提交与rebase”从git就绪。
我跑
git rebase --interactive HEAD~2
在结果编辑器中,我将pick更改为squash,然后save-quit,但由于出现错误,重基操作失败
没有之前的提交就不能“squash”
现在我的工作树已经达到了这个状态,我很难恢复。
命令git rebase——interactive HEAD~2失败:
交互式改基已经开始
git rebase -continue失败
没有之前的提交就不能“squash”
我经常使用git reset -mixed在你想合并的多次提交之前恢复一个基本版本,然后我做一个新的提交,这样可以让你提交最新的版本,确保你推送到服务器后你的版本是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重置——帮助:
--mixed
Resets the index but not the working tree (i.e., the changed files are
preserved but not marked for commit) and reports what has not been
updated. This is the default action.
如果你的主分支git日志看起来像下面这样:
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 checkout 77df2a40e53136c7a2d58fd847372 -b合并提交
现在,只需要从上次提交到这个新分支的更改中选择:git cherry-pick -n -x ac72a4308ba70cc42aace47509a5e。(如有冲突,应及时解决)
你在上次提交中的改变在第二次提交中。但是你仍然需要提交,所以首先添加你刚刚选择的更改,然后执行git commit—amend。
就是这样。如果你愿意,你可以在分支“merge -commits”中推送这个合并的版本。
此外,您现在可以在主分支中丢弃连续两次提交。只需将你的主分支更新为:
git checkout master
git reset --hard origin/master (CAUTION: This command will remove any local changes to your master branch)
git pull