Git rebase交互式最后n次提交

Tho*_* S. 18 git git-rebase

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

$ git rebase --interactive --onto <the-ID-of-the-first-commit-to-rewrite>
Run Code Online (Sandbox Code Playgroud)

用一个弹出VI

noop
Run Code Online (Sandbox Code Playgroud)

内容后跟评论信息.退出后,我的头重置为指定的提交.

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

Chr*_*aes 28

你应该使用

git rebase --interactive <sha1>
Run Code Online (Sandbox Code Playgroud)

这里<sha1>应该不会是第一的莎承诺要重写,但对SHA之前提交.

如果您的历史记录如下:

pick 43576ef last commit
...
pick 5116d42 first commit to rewrite
pick cb85072 last good commit
Run Code Online (Sandbox Code Playgroud)

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

git rebase -i cb85072
git rebase -i 5116d42^
Run Code Online (Sandbox Code Playgroud)

哪里

  • ^ 就是之前的提交.
  • -i 只是为了 --interactive


小智 23

要查看和重写最后一次n提交,请使用:

git rebase -i HEAD~n
Run Code Online (Sandbox Code Playgroud)

p, 选择 = 使用提交

f, fixup = like "squash", 但丢弃这个提交的日志信息

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


Ole*_*kyi 17

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


ber*_*gle 8

接受的答案是正确的

尽管如此,计算 n 次提交压缩并为 rebase 选择提交 id 是很棘手的

git rebase -i HEAD~[N]   // N is the number of commits, starting from the most recent one
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

git rebase -i HEAD~[7]

但是如果你有大量的承诺去壁球

git rebase -i [commit-id] // [commit-id] is the hash of the commit just before the first one 
Run Code Online (Sandbox Code Playgroud)

git rebase -i 6394dc


灵感来自


Mar*_*ska 8

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

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

例如

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

非交互式也能很好地工作 - 没有-i.