Git 无需打开编辑器的交互式变基

Git interactive rebase without opening the editor

Git 允许某些命令在不先打开编辑器的情况下创建或修改提交,例如:

git commit --amend --no-edit
git commit --fixup=HEAD^

我已将 rebase.autosquash 设置为 true,这样交互式变基的待办事项列表会自动重新排序。有没有一种方法可以立即执行变基,而无需先打开编辑器,例如:

git rebase -i --no-edit HEAD~3

TL;DR 答案:GIT_SEQUENCE_EDITOR=: git rebase -i HEAD~3

您无法从 运行 和 "sequence editor" 中停止 git rebase --interactive(这是 "sequence file" 上的编辑命令,其中包含各种选择等命令)。但是,如果您检查交互式 rebase 脚本:

$ vim $(git --exec-path)/git-rebase--interactive

您会在第 230 行左右找到这样的代码:

git_sequence_editor () {
    if test -z "$GIT_SEQUENCE_EDITOR"
    then
        GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
        if [ -z "$GIT_SEQUENCE_EDITOR" ]
        then
            GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
        fi
    fi

    eval "$GIT_SEQUENCE_EDITOR" '"$@"'
}

因此,您只需将序列编辑器设置为一个什么也不做然后成功的"edit"命令,例如shell内置的:命令,或 true 命令。

$GIT_SEQUENCE_EDITOR、已配置的 sequence.editor$GIT_EDITOR 中的任何一个都可以满足此要求,尽管显然最好的是第一个。)

作为 torek's (GIT_SEQUENCE_EDITOR=:) 的说明,请参阅 Git 2.21(2019 年 2 月):

设置 GIT_SEQUENCE_EDITOR 时,当隐式使用交互式变基机制的“git 变基”模式为 运行 时,命令错误启动,已更正.

参见 commit 891d4a0 (28 Jan 2019) by Phillip Wood (phillipwood)
(由 Junio C Hamano -- gitster -- in commit 69dd6e5 合并,2019 年 2 月 5 日)

implicit interactive rebase: don't run sequence editor

If GIT_SEQUENCE_EDITOR is set then rebase runs it when executing implicit interactive rebases which are supposed to appear non-interactive to the user.
Fix this by setting GIT_SEQUENCE_EDITOR=: rather than GIT_EDITOR=:.


Git 2.29(2020 年第 4 季度)记录了该环境变量。

参见 commit 902a126 (31 Aug 2020) by Philippe Blain (phil-blain)
(由 Junio C Hamano -- gitster -- in commit ed9d833 合并,2020 年 9 月 3 日)

doc: mention GIT_SEQUENCE_EDITOR and 'sequence.editor' more

Signed-off-by: Philippe Blain

The environment variable GIT_SEQUENCE_EDITOR, and the configuration variable 'sequence.editor', which were added in 821881d88d ("rebase -i": support special-purpose editor to edit insn sheet, 2011-10-17), are mentioned in the [git config](https://github.com/git/git/blob/902a126eca2d46b34dab822f1a1861bc2ce3cf48/Documentation/git-config.txt)<sup>([man](https://git-scm.com/docs/git-config))</sup> man page but not anywhere else.

Add GIT_SEQUENCE_EDITOR to the list of environment variables in git.

git 现在包含在其 man page 中:

GIT_SEQUENCE_EDITOR:

This environment variable overrides the configured Git editor when editing the todo list of an interactive rebase.
See also git rebase and the sequence.editor option in git config.

不使用 GIT_SEQUENCE_EDITOR 环境变量,您可以使用 -c 将配置传递给 git:

git -c sequence.editor=: rebase --autosquash --interactive origin/master

非常适合从编辑器中触发无编辑器变基(即,在 vim 中使用 fugitive 的 :Git 命令)。