MeshWorld India Logo MeshWorld.
HowTo Git Version Control Vim Nano Terminal 6 min read

How to Handle Git's Default Editor: Commit, Abort, and Configure

Cobie
By Cobie
How to Handle Git's Default Editor: Commit, Abort, and Configure

You ran git commit and suddenly you’re in an unfamiliar text editor. The cursor blinks. You’re expected to write a commit message, but you don’t know how to save or exit. This guide covers everything: handling the editor, writing good messages, aborting when needed, and avoiding the editor entirely.


Why Git Opens an Editor

When you run git commit without the -m flag, Git opens your default text editor to write a commit message. This allows:

  • Multi-line commit messages
  • Detailed descriptions
  • Reviewing what you’re committing

If no editor is configured, Git typically defaults to vi/vim on Linux/macOS or Notepad on Windows.


Quick Reference: Git Editor Commands

ActionCommand
Commit with message (no editor)git commit -m "Your message"
Open editor for commitgit commit
Abort commit in editorExit without saving
Change default editorgit config --global core.editor nano
Check current editorgit config core.editor
Amend last commitgit commit --amend

How to Write a Commit Message in the Editor

If Your Editor is Nano

  1. Write your commit message at the top
  2. Lines starting with # are comments (ignored)
  3. Press Ctrl+X to exit
  4. Press Y to confirm, Enter to save
  5. Git creates the commit

If Your Editor is vi/vim

  1. Press i to enter Insert mode
  2. Write your commit message at the top
  3. Press Esc to return to Normal mode
  4. Type :wq and press Enter to save and quit
  5. Git creates the commit

See How to Exit vi and Vim if you get stuck.

If Your Editor is Emacs

  1. Write your commit message at the top
  2. Press C-x C-s (Control+x, then Control+s) to save
  3. Press C-x C-c to exit
  4. Git creates the commit

See How to Exit Emacs if you get stuck.


How to Abort a Commit

Sometimes you open the editor and realize you don’t want to commit yet.

In Nano

Ctrl+X
N
Enter

Git aborts the commit, no changes saved.

In vi/vim

Esc
:q!
Enter

Git aborts the commit, no changes saved.

In Emacs

C-x C-c

When asked to save, type n or no, or press C-g then C-x C-c again.


How to Avoid the Editor Entirely

Method 1: One-liner commits

git commit -m "Fix login bug"

Method 2: Multi-line from command line

git commit -m "Add user authentication" -m "Implement JWT tokens for session management"

Method 3: Stage and commit in one command

git commit -am "Update documentation"

The -a flag stages all modified files (not new files).


How to Change Git’s Default Editor

git config --global core.editor nano

To Vim

git config --global core.editor vim

To VS Code

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

To Emacs

git config --global core.editor emacs

Check your current editor

git config core.editor

System-wide default (environment variable)

# Add to ~/.bashrc or ~/.zshrc
export EDITOR=nano
export GIT_EDITOR=nano

Writing Good Commit Messages

Git commit messages follow a convention:

Short summary (50 chars or less)

More detailed explanation if needed. Wrap at 72 characters.
Explain the why, not just the what. Reference issues:

Fixes #123

Tips

  • First line: Brief summary, imperative mood (“Fix bug” not “Fixed bug”)
  • Blank line: Separate summary from body
  • Body: Explain what and why, not how (code shows how)
  • References: Link to issues or tickets

Common Stuck Scenarios

”I’m in the editor but don’t see what I’m committing”

Problem: The commented lines show the changes, but they’re hard to read.

Solution: In vim, you can scroll. In nano, the comments are at the bottom. Look for lines starting with #.

”I saved but Git says ‘Aborting commit due to empty message’”

Problem: You didn’t write anything on the first line (all comments).

Solution: Reopen the editor and type a commit message on the first line.

”I accidentally committed with a bad message”

Fix the last commit:

git commit --amend

This reopens the editor. Fix the message, save, exit. If you’ve already pushed, you’ll need to force push:

git push --force-with-lease

“I want to see the diff while writing the message”

Git shows the diff in comments at the bottom. To see it while editing:

In vim:

:sp    (split window, scroll to see comments)

In nano: Press Down arrow to scroll and see the comments.


Interactive Rebase and the Editor

Interactive rebase (git rebase -i) opens the editor to let you choose actions:

pick abc1234 Commit message 1
pick def5678 Commit message 2
pick ghi9012 Commit message 3

Common actions

  • pick — Keep commit as-is
  • reword — Change commit message (opens editor)
  • squash — Combine with previous commit
  • drop — Remove commit
  • edit — Stop to amend commit

Save and exit to proceed. If you get stuck, exit the editor the same way as with commits.


FAQ

Can I disable the editor completely?

Not really, but you can use -m for all commits or set up aliases:

# In ~/.bashrc or ~/.zshrc
alias gc='git commit -m'

Then use: gc "Your message"

What if my editor is set to something I don’t have installed?

Git will error. Fix it:

git config --global core.editor nano

Why does git commit --amend open the editor?

To let you edit the commit message. To keep the message:

git commit --amend --no-edit

How do I commit with an empty message?

git commit --allow-empty-message -m ""

Not recommended, but possible.

Can I use a GUI editor for Git commits?

Yes:

# VS Code
git config --global core.editor "code --wait"

# Sublime Text
git config --global core.editor "subl -n -w"

# Atom (discontinued but still used)
git config --global core.editor "atom --wait"

Summary: The Commands You Need

SituationCommand
Commit without editorgit commit -m "message"
Abort commit in editorExit without saving (:q!, Ctrl+XN, or C-x C-cn)
Change editor to Nanogit config --global core.editor nano
Change editor to Vimgit config --global core.editor vim
Fix last commit messagegit commit --amend
Stage all and commitgit commit -am "message"

Need help with the editor itself? Check How to Exit vi and Vim, How to Exit Nano, or How to Exit Emacs.