如何抑制 `git rebase --continue` 的编辑器?

How to suppress the editor for `git rebase --continue`?

我经常 rebasing interactive 以在历史记录中进行微小的更改(例如删除空白行或编辑一行)。在大多数情况下,这些更改是基于一些同行评审。

起初我是这样改的:

git rebase --interactive 83bbeb27fcb1b5e164318fa17c55b7a38e5d3c9d~
# replace "pick" by "edit" on the first line
# modify code
git commit --all --amend --no-edit
git rebase --continue

如果以下提交之一存在合并冲突,我将解决它们并执行此操作:

git rebase --continue
# the commit message is shown, but does not have to be changed, so I just save and exit the editor
git rebase --continue
# the commit message is shown, but does not have to be changed, so I just save and exit the editor
git rebase --continue
# the commit message is shown, but does not have to be changed, so I just save and exit the editor
...

是否有类似 git rebase --continue--no-edit 参数来避开编辑器(类似于 git commit)?

第一种方法,通过Git配置:

git -c core.editor=true rebase --continue

或者,使用环境变量:

GIT_EDITOR=true git rebase --continue

这将覆盖 git 用于消息确认的编辑器。 true 命令简单地以零退出代码结束。它使 git 继续 rebase 就像用户关闭交互式编辑器一样。

在 Windows,您需要 use CMD /V /C

cmd /V /C "set "GIT_EDITOR=true" && git rebase --continue

在这两个 OS 中,您可以使用别名

# Linux
alias grc='GIT_EDITOR=true git rebase --continue'

# Windows
doskey grc=cmd /V /C "set "GIT_EDITOR=true" && git rebase --continue"

然后一个简单的 grc 就足以继续变基 而不会 编辑器弹出。


要全局设置(没有别名),可以考虑 :

specifying the core.commentChar detected from the .git/rebase-merge/message (here @):

git --global core.commentChar @

还有 ,但您不能始终将其保留在该值,因为这会破坏您想要编辑器的所有变基操作。

这就是为什么使用别名仍然是最灵活和准确的方法,而不是依赖可能会破坏其他 rebase 编辑器用例的全局设置。

使用 true 作为编辑器的技巧也可以通过这样的 git 别名来实现:

[alias]
    continue = "-c core.editor=true rebase --continue"

那你就用git continue代替git rebase --continue