Shu*_*ary 23 git rebase squash
如果我在以下情况,
$ git log --oneline
* abcdef commit #b
* 123456 commit #a
Run Code Online (Sandbox Code Playgroud)
我知道我总能跑
$ git reset HEAD~
$ git commit --amend
Run Code Online (Sandbox Code Playgroud)
但是,我试着跑
$ git rebase -i HEAD~2
Run Code Online (Sandbox Code Playgroud)
但我明白了
fatal: Needed a single revision
invalid upstream HEAD~2
Run Code Online (Sandbox Code Playgroud)
因此我的问题是:有没有办法git rebase用来压缩这两个提交?
jub*_*0bs 31
您想要重新绑定到master分支的根提交.更具体地说,要压缩两个提交,您需要运行
git rebase -i --root
Run Code Online (Sandbox Code Playgroud)
然后替换squash为pick在弹出的编辑器的缓冲器中的第二行:
pick 123456 a
squash abcdef b
Run Code Online (Sandbox Code Playgroud)
有关该标志的更多详细信息,请参阅git-rebase手册页:
--root重新引用可以访问的所有提交
<branch>,而不是使用<upstream>.这允许您在树枝上重新定义根提交.[...]
# 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
Run Code Online (Sandbox Code Playgroud)
gui*_*ido 10
看来这个参数可能会有所帮助:
Run Code Online (Sandbox Code Playgroud)--root重新引用从<branch>可到达的所有提交,而不是用<upstream>限制它们.这允许您在树枝上重新定义根提交.
这应该让你压扁(我猜你真的想要修复)你的第二次提交到第一次:
git rebase --root -i
Run Code Online (Sandbox Code Playgroud)
注意了解--root选项的作用,因为在你的情况下它会满足你的需求,但是在分支中使用时可能会很棘手,因为它会重新定位到历史中可以到达的最远的祖先(即树的根); 所以rebase --root将变基z上a通A-B-D-E-X-Y-Z:
master A-B-C
\
upstream D-E
\
current branch X-Y-Z
Run Code Online (Sandbox Code Playgroud)
我来到这个问题是为了寻找标题的答案。在一个非常大的存储库上,接受的答案会为主分支(又名:master)中的每个提交生成交互式变基,而不仅仅是给定分支。对于来到这里的其他人,替代方法是使用父分支名称(或提交):
\n\ngit rebase -i <base_branch_name>
在深入研究 git 文档并找到这个(https://git-scm.com/docs/git-rebase#_interactive_mode)后我意识到了这一点:
\n\n\n\n\n从您想要按原样保留的最后一次提交开始:
\n\n\n\n
git rebase -i <after-this-commit>编辑器将启动当前分支中的所有提交(忽略合并提交),这些提交是在给定提交之后的。
\n
假设您有从 master 分支出来的branch_a,并且您希望以交互方式对 上的所有提交进行变基branch_a。为此,您需要执行以下操作:
git rebase -i master
或者,如果您想从中间(或任何提交)开始,您可以执行以下操作:
\n\ngit rebase -i <some-commit-hash-in-the-middle>
另一种常见的形式是如果你知道“我想从现在的位置重新调整 5 个提交”
\n\ngit rebase -i HEAD~5
希望这可以帮助其他人在 git rebase 打开一个包含数千行提交的编辑器时避免心脏病发作。\xe2\x94\x80=\xe2\x89\xa1\xce\xa3((\xe3\x81\xa4\xef\xbc\x9e\xef\xbc\x9c)\xe3\x81\xa4
\n| 归档时间: |
|
| 查看次数: |
7720 次 |
| 最近记录: |