如果我处于以下情况,
$ git log --oneline
* abcdef commit #b
* 123456 commit #a
我知道我总能跑
$ git reset HEAD~
$ git commit --amend
但是,我试着跑
$ git rebase -i HEAD~2
但是我得到了
fatal: Needed a single revision
invalid upstream HEAD~2
因此,我的问题是:是否有一种方法可以使用
git rebase
来压缩这两个提交? 最佳答案
您想基于master
分支的根提交。更具体地说,要压缩两个提交,您需要运行
git rebase -i --root
然后在 pop 的编辑器缓冲区的第二行中用
squash
替换pick
:pick 123456 a
squash abcdef b
我建议您引用
git-rebase
man page以获取有关该标志的更多详细信息:根的交互式变基示例
# Set things up
$ mkdir testgit
$ cd testgit
$ git init
# Make two commits
$ touch README
$ git add README
$ git commit -m "add README"
$ printf "foo\n" > README
$ git commit -am "write 'foo' in README"
# Inspect the log
$ git log --oneline --decorate --graph
* 815b6ca (HEAD -> master) write 'foo' in README
* 630ede6 add README
# Rebase (interactively) the root of the current branch:
# - Substitute 'squash' for 'pick' on the second line; save and quit the editor.
# - Then write the commit message of the resulting commit; save and quit the editor.
$ git rebase -i --root
[detached HEAD c9003cd] add README; write 'foo' in README
Date: Sat May 16 17:38:43 2015 +0100
1 file changed, 1 insertion(+)
create mode 100644 README
Successfully rebased and updated refs/heads/master.
# Inspect the log again
$ git log --oneline --decorate --graph
* c9003cd (HEAD -> master) add README; write 'foo' in README
关于git - 如何在分支的第一个(根)提交上使用交互式基础库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30277149/