Git rebase interactive 最后 n 次提交

Git rebase interactive the last n commits

我在我的功能分支中做了一堆未推送的提交,现在想重新排序并部分压缩属于可视化的提交。我认为解决方案不知何故在于 Git 交互,但如何调用它?

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

只是弹出带有

的VI
noop

内容后跟评论信息。退出后,我的头像重置为指定的commit。

如何正确触发交互式变基以修改自某个提交以来的提交?

我想念你指令中的动作rebase

git rebase -i <id-of-commit>

你应该使用

git rebase --interactive <sha1>

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

如果您的历史是这样的:

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

那么你可以有不同的方式来指示要变基的提交:

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

其中

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

您还可以从上次提交后退一些提交。例如,如果你想 rebase 最后 5 次提交,你可以使用这个命令: git rebase -i HEAD~5.

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

git rebase -i HEAD~n

p, pick = 使用提交

f, fixup = 喜欢 "squash",但丢弃此提交的日志消息

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

接受的答案是正确的

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

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

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

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

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

例如

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

在没有 -i.

的情况下,非交互式也能很好地工作