61

I'm often rebasing interactive to make tiny changes in the history (for example removing a blank line, or editing one line). In most cases those changes are based on some peer review.

At first I do my change like that:

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

If there are merge conflicts in one of the following commits, I resolve them and do this:

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
...

Is there anything like a --no-edit argument to git rebase --continue that avoids the editor (similar to git commit)?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
slartidan
  • 20,403
  • 15
  • 83
  • 131
  • 2
    Set your preferred editor to a does-nothing-and-succeeds command, such as `true`. Git will invoke it, it will do nothing and succeed, and Git will re-use the existing commit message. Hence: `GIT_EDITOR=true git rebase --continue`. – torek Apr 19 '17 at 08:10
  • @torek Is there a way to apply the change of the editor to only *one* git command? I tried `;` (like that: `GIT_EDITOR=true;git rebase --continue`), but that did not work. (I'm using a mac, btw) – slartidan Apr 19 '17 at 08:20
  • That (environment setting via shell `VAR=val cmd...`) *does* affect just the one command, plus anything it runs itself. To affect multiple commands you must set the variable and "export" it, which in most shells is `export VAR=value; command1; command2; ...` – torek Apr 19 '17 at 08:27

2 Answers2

59

First approach, through Git configuration:

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

Or, with environment variables:

GIT_EDITOR=true git rebase --continue

This will override the editor that git uses for message confirmation. true command simply ends with zero exit code. It makes git continue rebase as if user closed interactive editor.

On Windows, you would need to use CMD /V /C

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

In both OS, you can use aliases

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

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

Then a simple grc will be enough to continue the rebase without the editor popping up.


David Gardiner adds in the comments:

In PowerShell, use:

$env:GIT_EDITOR = "true -and git rebase --continue"

then tidy up with

 $env:GIT_EDITOR = ""

To set that globally (without alias), maybe consider this approach:

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

git --global core.commentChar @

There is also GIT_SEQUENCE_EDITOR=:, but you cannot always leave it at that value, because that would break all the rebase operation where you want an editor.

That is why having an alias remains the most flexible and accurate approach, rather than relying on a global setting which might break other rebase editor use cases.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Setting env var like `var=val` works only for shell. In order to make ti work for all processes that will be started after that you need to call `export var`. Doing everything as single command can be `export var=val`. But it would be bad idea to override `GIT_EDITOR` for all other invocations of `git`. So the final recommendation (`var=val command`) is the best one because it has no negative side effect. – Victor Yarema Apr 18 '20 at 19:32
  • @VictorYarema Thank you for the comment and the edit: this answer is clearer now, thanks to you. – VonC Apr 18 '20 at 20:32
  • Is there a way to config it globally? I know it's possible because this is how it worked on my old computer.. – Mosh Feu Nov 01 '21 at 16:34
  • 1
    @MoshFeu Maybe this: https://stackoverflow.com/a/64350016/6309 – VonC Nov 01 '21 at 16:47
  • Thanks! I will check it – Mosh Feu Nov 01 '21 at 18:35
  • In PowerShell, use: `$env:GIT_EDITOR = "true -and git rebase --continue"`, then tidy up with `$env:GIT_EDITOR = ""` – David Gardiner Sep 08 '22 at 01:51
  • @DavidGardiner Thank you for your feedback. I have included your comment in the answer for more visibility. – VonC Sep 08 '22 at 05:25
17

The trick of using true as an editor can also be accomplished with a git alias like this:

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

Then you use git continue instead of git rebase --continue.

Jared Neil
  • 366
  • 3
  • 7