Git Tips #2 - Change editor for interactive Git rebase

This is the overdue second post, in my series of Git tips for both new and experienced Git users. Some are commands I use, but not often enough to remember the syntax. Others are config changes I like to make to my environment.

The List

The full list of my Git tips is here.

What is an interactive rebase?

Rebasing is a powerful and advanced Git concept. I won't write too much about it here, as others have covered it already, but in essence it allows you to re-write the history of your repository.

Why would anyone want to do this? Rebasing can be used to clean up your commit history on a branch before pushing it. You may want to squash some commits or alter your commit messages for example. It is very important to only rebase branches which you haven't shared with others yet.

It is interactive because Git will open a text editor to ask for input. The default editor is VIM. VIM is a fine editor if you know how to use it, but I prefer to use Sublime Text.

The Command

Open a command prompt and type the following command:

git config --global core.editor "'{path to editor}' -n -w"  

Sublime Text

For Sublime Text on Windows, this is the command:

git config --global core.editor "'C:\Program Files\Sublime Text 3\subl.exe' -n -w"  

What does this command do?

  • git config is used to set global and repository settings.
  • core.editor is the setting which tells Git which editor to use.
  • -n tells Git to open a new editor window. Omit this flag you want Git to reuse the Sublime window you already have open.
  • -w tells Git to wait until the commit message is saved.

Atom

For Atom on Windows, this is the command:

git config --global core.editor "atom.cmd --wait"  

Notepad++

For Notepad++, run this command:

git config --global core.editor "'C:\Program Files\Notepad++\notepad++.exe' -nosession -notabbar"  

VS Code

For VS Code, run this command:

git config --global core.editor "code --wait"  

References