交互式变基和普通变基有什么区别?

15

交互式变基和普通变基有何不同?例如:

git rebase -i HEAD~3

不使用-i进行变基:


git rebase HEAD~3

如果你尝试运行 git rebase --help 并阅读文档,你会发现一些很好的例子,说明了为什么以及如何使用它。 - Zoe Edwards
一个是交互式的,另一个不是。 - evolutionxbox
4个回答

10
作为Thomas Edwards的评论所说,这里的文档非常有帮助。还有Pro Git书籍(特别是关于变基重写历史的章节)也很有用。
在其核心,变基会检出一个根提交并逐个应用一系列提交。
当您执行常规变基(git rebase HEAD~3)时,这将自动发生。
然而,当您执行交互式变基(git rebase -i HEAD~3)时,您将有机会编辑提交。
这可以看作是修改提交消息,将提交合并在一起,编辑提交中的更改甚至完全删除提交!

还有一些其他的区别。如果rebase是交互式的,则会忽略合并策略标志。 - Tom Anderson

3
  • 普通变基(Normal Rebase):当前分支简单地变基,不需要用户反馈。
  • 交互式变基(Interactive Rebase):用户可以在当前分支中对提交进行操作。用户可以重新排序提交和执行以下其他选项:

命令:

  • p, pick = 使用提交
  • r, reword = 使用提交,但编辑提交消息
  • e, edit = 使用提交,但停止以进行修补
  • s, squash = 使用提交,但融合到前一个提交中
  • f, fixup = 类似于“squash”,但丢弃此提交的日志消息
  • x, exec = 使用 shell 运行命令(行的其余部分)
  • d, drop = 删除提交

2

普通变基

它不需要任何用户输入,因此选择分支中的所有提交而不进行任何修改并应用所有提交。

交互式变基

它会在你面前打开编辑器,以便我们可以按照自己的方式对每个提交进行任何修改。就像下面给出的一样。

交互式变基提交编辑器

pick <commit_hash> <commit_message>
pick 42522f0 add stack implementation

# Rebase 6aa416b..42522f0 onto 6aa416b (1 command)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.

在正常的变基中,它会自动完成,并默认选择所有提交,而不是任何修改(变基、编辑或删除等)。
如果您只有一个提交,则最好使用“正常变基”,反之亦然。

1
交互式变基会打开一个编辑器,其中列出了即将更改的提交记录列表。该列表接受命令,允许用户在启动变基操作之前编辑列表。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接