Sub topic 9
Choosing your commit message editor
This chapter assumes Git is installed, you are comfortable in a terminal, and you have already set user.name and user.email. It covers which editor Git opens when it needs a message from you, and how to change that editor.
When Git opens an editor
Git starts an editor when it needs text from you and you haven't supplied it on the command line. Running git commit without -m is the usual case. Git opens a temporary file called COMMIT_EDITMSG, you write the message, and you save and close. Git reads the file and finishes the commit. Interactive rebase and annotated tags use the same mechanism.
If you have never configured an editor, Git falls back to vi on most systems. Many people get stuck in it on their first commit. To leave Vim without committing, type :q! and press Enter. An empty message aborts the commit.
Setting core.editor
The core.editor key tells Git which command to run. Set it once at the global scope:
git config --global core.editor "code --wait"The value is a command line. Git appends the path of the temporary file to it. The --wait flag matters for graphical editors. Without it, code hands the file to a running window and exits immediately. Git then sees an empty message and aborts the commit. With --wait, the command blocks until you close the file's tab or window.
Terminal editors block by default, so they need no extra flag. These values are common:
| Editor | Value for core.editor |
|---|---|
| Visual Studio Code | `code --wait` |
| Nano | `nano` |
| Vim | `vim` |
| Sublime Text | `subl -n -w` |
If the editor is not on your PATH, give the full path to the executable. Quote the whole value if the path contains spaces.
How Git picks an editor
core.editor is not the only input. Git checks several places and uses the first one that is set.
The environment variable GIT_EDITOR beats core.editor. core.editor beats VISUAL and EDITOR. If your setting seems ignored, check whether GIT_EDITOR is exported in your shell profile.
Confirming the value
Use the origin flag from the inspection chapter to see the value and the file that supplies it:
git config --show-origin --get core.editorThe output shows a path such as file:/home/ada/.gitconfig followed by the value. If nothing prints, no config file sets the key. Git is then using an environment variable or the default.
Common problems
- Commit aborts with an empty message. A graphical editor is missing its wait flag. Add
--waitor the editor's equivalent. - "command not found" when committing. The editor is not on your
PATH. Use a full path or fix yourPATH. - The wrong editor opens.
GIT_EDITORmay be set, or a localcore.editormay override your global one. Run the--show-origincommand from inside the repository. - You only want to skip the editor once. Use
git commit -m "message", orgit commit --no-editwhen amending.
Try it
Exercise
You set core.editor to "code" on your machine. Every git commit now prints "Aborting commit due to empty commit message" right after VS Code appears. What is wrong, and what command fixes it?
Show answer
The code command returns immediately after handing the file to VS Code, so Git reads an empty file before you type anything. Add the wait flag: git config --global core.editor "code --wait".
Exercise
git config --show-origin --get core.editor shows nano, but Vim opens when you commit. Which setting is the likely cause, and where would you look?
Show answer
The GITEDITOR environment variable is probably set. It takes precedence over core.editor. Check it with echo $GITEDITOR and look in your shell profile for an export line.