2016-11-20 250 views

回答

1

你只需要--root标志(可用自1.7.12的Git,即对任何人,除某些无名从不更新的咳嗽* Centos的*咳嗽 Linux发行版)。

考虑到挤压一个的结果是(排序提交的不相当遵守这个简单的代数运算,但它适用于我们的情况下)只是A + B。因此,由于B + A = A + B,无论您是否将#1提交到提交#2或提交#2提交到提交#1,都没有关系。当然,这并不包括压制在一起的提交的消息,所以让我们仔细看看。

假设您已运行git rebase -i --root以便能够重新绑定包括根提交在内的所有内容。现在,您可以:

pick cb57fdd initial commit 
pick 478acbd second commit 

更改第二个到squash和写入文件并退出编辑器。 Git的南瓜两个一起提交,并再次带来了你的编辑器,你可以看到:

# This is a combination of 2 commits. 
# This is the 1st commit message: 

initial commit 

# This is the commit message #2: 

second commit 

# Please enter the commit message for your changes. Lines starting 
# with '#' will be ignored, and an empty message aborts the commit. 
# 
# Date:  Sat Nov 19 14:44:07 2016 -0800 
# 
# interactive rebase in progress; onto 45bc10c 
# Last commands done (2 commands done): 
# pick cb57fdd initial commit 
# squash 478acbd second commit 

(精确的细节取决于你的Git版本,以及当然作为提交内容)。

既然你是在你最喜欢的编辑器中,很容易构造你想要的任何新的提交消息。例如,你可以删除整个第一个提交的消息,如果这是你想要的。做到这一点,写下文件,然后退出编辑器,你就完成了。

0
  • 见分行的名称(比如,branch-1
    $ git branch
  • 见第一提交哈希并保存其他人(比如第一个提交)
    $ git log
  • 退房得第一代提交
    $ git checkout first-commit
  • 更改提交信息
    $ git commit --amend -am 'new-commit-msg' --allow-empty
  • 现在保存新的提交哈希值(比方说,新commmit)
    $ git log
  • 回到你的 '分支1' 分支 git checkout branch-1
  • 通过new-commit
    $ git replace first-commit new-commit
  • 重写替换first-commit所有期货的提交基于上更换
    $ git filter-branch -- --all
  • 用力推动你的变化
    git push -f origin HEAD
+0

这是在第一次提交中编辑* message *的一种方法,但它不会压缩与另一次提交一起提交的内容,这是OP要求的。 (如果你打算使用'git filter-branch',你也可能不需要使用'git filter-branch',因为你可以在'git filter-branch'命令中完成整个工作,使用'--msg-filter',它由''if [$ GIT_COMMIT = ];然后cat/tmp/newmsg; else cat; fi''或类似。) – torek