Git interactive rebase without opening the editor
Asked Answered
F

3

49

Git allows certain commands to create or modify commits without opening the editor first, for example:

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

I have set rebase.autosquash to true, so that the todo list for an interactive rebase is automatically reordered. Is there a way to immediately perform the rebase, without opening the editor first, something like:

git rebase -i --no-edit HEAD~3
Filmer answered 17/3, 2015 at 8:43 Comment(2)
I'm curious, why would you not want the editor?Boley
@TimCastelijns Maybe for batch operations on the server side. The question is, why does --autosquash require --interactive...Leucas
H
55

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

You can't stop git rebase --interactive from running the "sequence editor" (that's the edit command on the "sequence file" containing the various pick, etc., commands). However, if you examine the interactive rebase script:

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

you'll find code like this near line 230 or so:

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" '"$@"'
}

Thus, you simply need to set the sequence editor to an "edit" command that does nothing and then succeeds, such as the shell's built-in : command, or the true command.

(Any of $GIT_SEQUENCE_EDITOR, the configured sequence.editor, or $GIT_EDITOR will suffice for this, though the obvious best one to use is the first.)

Harms answered 17/3, 2015 at 8:59 Comment(2)
This stops it from running, but it was not equivalent to running the editor and typing :wq -- that is, the editor didn't run, but the rebase didn't complete. I ran it within a git alias to a shell function, if that matters: cadd = "!f() { git commit --fixup=$1; GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash $1^; }; f"Matrimony
@Jonah: Odd ... do you have an example with details?Harms
J
20

Instead of using the GIT_SEQUENCE_EDITOR environment variable, you can use -c to pass configuration to git:

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

Works well for triggering editor-free rebases from within your editor (i.e., using fugitive's :Git command in vim).

Jaw answered 19/6, 2021 at 16:38 Comment(1)
For information, in case of an empty commit, one may have to run this command a few times.Raveaux
L
9

As an illustration of torek's solution (GIT_SEQUENCE_EDITOR=:) see Git 2.21 (Feb. 2019):

When GIT_SEQUENCE_EDITOR is set, the command was incorrectly started when modes of "git rebase" that implicitly uses the machinery for the interactive rebase are run, which has been corrected.

See commit 891d4a0 (28 Jan 2019) by Phillip Wood (phillipwood).
(Merged by Junio C Hamano -- gitster -- in commit 69dd6e5, 05 Feb 2019)

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 (Q4 2020) documents that environment variable.

See commit 902a126 (31 Aug 2020) by Philippe Blain (phil-blain).
(Merged by Junio C Hamano -- gitster -- in commit ed9d833, 03 Sep 2020)

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 now includes in its 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.

Luong answered 3/3, 2019 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.