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
| Action | Command |
|---|---|
| Commit with message (no editor) | git commit -m "Your message" |
| Open editor for commit | git commit |
| Abort commit in editor | Exit without saving |
| Change default editor | git config --global core.editor nano |
| Check current editor | git config core.editor |
| Amend last commit | git commit --amend |
How to Write a Commit Message in the Editor
If Your Editor is Nano
- Write your commit message at the top
- Lines starting with
#are comments (ignored) - Press
Ctrl+Xto exit - Press
Yto confirm,Enterto save - Git creates the commit
If Your Editor is vi/vim
- Press
ito enter Insert mode - Write your commit message at the top
- Press
Escto return to Normal mode - Type
:wqand pressEnterto save and quit - Git creates the commit
See How to Exit vi and Vim if you get stuck.
If Your Editor is Emacs
- Write your commit message at the top
- Press
C-x C-s(Control+x, then Control+s) to save - Press
C-x C-cto exit - 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
To Nano (recommended for beginners)
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-isreword— Change commit message (opens editor)squash— Combine with previous commitdrop— Remove commitedit— 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
| Situation | Command |
|---|---|
| Commit without editor | git commit -m "message" |
| Abort commit in editor | Exit without saving (:q!, Ctrl+X→N, or C-x C-c→n) |
| Change editor to Nano | git config --global core.editor nano |
| Change editor to Vim | git config --global core.editor vim |
| Fix last commit message | git commit --amend |
| Stage all and commit | git 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.
Related Articles
Deepen your understanding with these curated continuations.
Terminal Text Editors Compared: vi/vim vs Nano vs Emacs
Choose the right terminal text editor — compare vi/vim, Nano, and Emacs by learning curve, power, use cases, and when to use each one.
How to Exit Nano: Save, Quit, and Force Close
Complete guide to exiting Nano editor — save changes, quit without saving, force quit, and handle common stuck scenarios with clear examples.
How to Exit vi and Vim: Save, Quit, and Force Close
Complete guide to exiting vi/vim — save changes, quit without saving, force quit, and handle common stuck scenarios with clear examples.