Git rebase 交互式最后 n 次提交

问题描述 投票:0回答:7

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

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

只需弹出带有

的 VI
noop

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

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

git git-rebase
7个回答
77
投票

你应该使用

git rebase --interactive <sha1>

其中

<sha1>
应该 not 是您要重写的第一个提交的 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
  • 的缩写

49
投票

您还可以从上次提交后退一定数量的提交。例如,如果您想对最近 5 次提交进行变基,您可以使用以下命令:

git rebase -i HEAD~5


46
投票

要查看并重写最后的

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


12
投票

接受的答案是正确的

尽管如此,计算挤压的 n 次提交并选择用于变基的提交 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


灵感来自


8
投票

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

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

例如

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

在非交互方式下也能很好地工作 - 没有

-i


1
投票

我想念您指示中的动作

rebase

git rebase -i <id-of-commit>

0
投票

对于任何尝试跑步的人:

git rebase -i <commit_id>^

并出现以下错误:

zsh: no matches found: <commit_id>^

解决方案是用引号将提交标识符括起来,例如:

git rebase -i "<commit_id>^"
© www.soinside.com 2019 - 2024. All rights reserved.