Git rebase交互式最后n次提交

时间:2017-01-04 13:16:34

标签: git git-rebase

我在我的功能分支中做了一堆未经提交的提交,现在想要重新排序并部分压缩属于提交的视觉效果。我认为解决方案在某种程度上取决于Git交互式,但是如何调用它?

$ git rebase --interactive --onto <the-ID-of-the-first-commit-to-rewrite>

只需用

弹出VI
noop

内容后跟注释信息。退出后,我的头重置为指定的提交。

如何正确触发交互式rebase以修改自某次提交以来的提交?

6 个答案:

答案 0 :(得分:19)

你应该使用

git rebase --interactive <sha1>

其中<sha1>应该是你要重写的第一个提交的sha,而是之前提交的sha。

如果您的历史记录如下:

pick 43576ef last commit
...
pick 5116d42 first commit to rewrite
pick cb85072 last good commit

那么您可以通过不同的方式指示要进行rebase的提交:

git rebase -i cb85072
git rebase -i 5116d42^

其中

  • ^表示之前的提交。
  • -i只是--interactive
  • 的缩写

答案 1 :(得分:13)

您也可以通过一些提交退回上一次提交。例如,如果要重新设置最近5次提交,可以使用以下命令: git rebase -i HEAD~5

答案 2 :(得分:2)

要查看并重写最近的n个提交,请使用:

git rebase -i HEAD~n

p,选择=使用提交

f,fixup =类似于“ squash”,但丢弃此提交的日志消息

https://www.freecodecamp.org/forum/t/how-to-squash-multiple-commits-into-one-with-git-squash/13231

答案 3 :(得分:2)

如果您希望使用分支B的最后N次提交以交互方式将其分支到分支A,则通常可以这样做:

git rebase -i --onto A B~N B

例如

git rebase -i --onto master feature~3 feature

在没有-i的情况下,也可以非交互式地很好地工作。

答案 4 :(得分:1)

我在你的指示中错过了RuntimeException行动:

rebase

答案 5 :(得分:0)

接受的答案是正确的

尽管,计数n次提交到壁球并选择提交ID进行重设很麻烦

git rebase -i HEAD~[N]   // N is the number of commits, starting from the most recent one

enter image description here

git rebase -i HEAD~[7]

但是如果您有大量的壁球

git rebase -i [commit-id] // [commit-id] is the hash of the commit just before the first one 

git rebase -i 6394dc


Inspired by

相关问题