git commit
Record changes to the repository
The 'git commit' command creates a new commit containing the current contents of the index (staging area) and a log message describing the changes. The new commit is a direct child of HEAD and becomes the new tip of the current branch. Each commit is a permanent snapshot in the project history that can be revisited, compared, or restored at any time.
Synopsis
git commit [-a] [-m <msg>] [--amend] [--no-edit] [-v] [-n] [--dry-run] [-s] [-S] [--] [<pathspec>...]Options
Choosing the Right Option
Message Options
Different ways to provide or edit the commit message
| Command | Source | Opens Editor | Use Case |
|---|---|---|---|
git commit -m "msg" | Inline | ✗ | Quick commits with short messages |
git commit -F file | File | ✗ | Pre-written message from file |
git commit -t file | Template | ✓ | Start with a template in editor |
git commit -e | Editor | ✓ | Force editor even with -m |
git commit -C <commit> | Copy | ✗ | Reuse message from another commit |
git commit -c <commit> | Copy+Edit | ✓ | Edit message from another commit |
Message Cleanup
How the commit message is processed before saving
| Command | Mode | Strips Comments | Strips Whitespace | Best For |
|---|---|---|---|---|
--cleanup=strip | strip | ✓ | ✓ | Most commits |
--cleanup=whitespace | Whitespace | ✗ | ✓ | Keep # comments |
--cleanup=verbatim | Verbatim | ✗ | ✗ | Exact message preservation |
--cleanup=scissors | Scissors | Only below scissors line | ✓ | Keep # lines when using the editor |
Author & Date
Override who made the commit and when
| Command | Overrides | Format | Typical Use |
|---|---|---|---|
--author="Name <email>" | Author | Name <email> | Committing on someone's behalf |
--date="date" | Author date | ISO 8601 / relative | Backdating commits |
--reset-author | Both | Current user/time | Taking ownership of applied patch |
Amending & Fixing History
Options for modifying existing commits
| Command | Creates | Modifies HEAD | Use With |
|---|---|---|---|
git commit --amend | Replacement | ✓ | Fix last commit message or content |
git commit --fixup=<sha> | fixup! commit | ✗ | git rebase -i --autosquash |
git commit --squash=<sha> | squash! commit | ✗ | git rebase -i --autosquash |
What Gets Committed
Control which files are included in the commit
| Command | Stages First | Commits | Working Dir |
|---|---|---|---|
git commit | ✗ | Staged only | Unchanged |
git commit -a | Tracked files | All tracked | Unchanged |
git commit -i <file> | Specified | Staged + file | Unchanged |
git commit -o <file> | Specified only | Only file | Other staged excluded |
git commit -p | Interactive | Selected hunks | Unchanged |
GPG Signing
Cryptographically sign commits for verification
| Command | Action | Key Used | When to Use |
|---|---|---|---|
git commit -S | Sign | Default GPG key | Prove commit authenticity |
git commit -S<keyid> | Sign | Specific key | Use alternate signing key |
git commit --no-gpg-sign | Don't sign | None | Override commit.gpgSign=true |
Hook Control
Bypass or control pre-commit and post-commit hooks
| Command | Skips | Risk Level | When to Use |
|---|---|---|---|
git commit --no-verify | pre-commit + commit-msg | Medium | Emergency fix, fix hooks later |
git commit --no-post-rewrite | post-rewrite | Low | Skip slow post-amend processing |
Dry-Run & Output Format
Preview what would be committed without actually committing
| Command | Format | Parseable | Shows |
|---|---|---|---|
--dry-run | Long (default) | ✗ | Full status with hints |
--dry-run --short | Short | ✓ | Compact status codes |
--dry-run --porcelain | Porcelain | ✓ | Stable format for scripts |
--dry-run --branch | With branch | ✓ | Adds tracking info to short |
--dry-run -z | NUL-separated | ✓ | Safe for special filenames |
Editor Status Display
Control what appears in the editor when writing commit message
| Command | In Editor | Effect |
|---|---|---|
git commit --status | Status as comments | See files being committed (default) |
git commit --no-status | No file list | Cleaner editor, less distraction |
git commit -v | Full diff | Review exact changes while writing |
git commit -q | Normal editor | Suppress summary after commit |
Special Commits
Create commits in unusual situations
| Command | Allows | Creates Commit | Use Case |
|---|---|---|---|
--allow-empty | No changes | ✓ | CI triggers, branch markers |
--allow-empty-message | No message | ✓ | Automated scripts (not recommended) |
--trailer "Key: value" | Metadata | ✓ | Co-authored-by, Fixes, Reviewed-by |
Workflow Examples
git add .Stage your changes
git commit -m "Fix typo in README"Commit with a short message
git add -AStage all changes
git commitOpens editor for multi-line message
(Write: subject line, blank line, detailed body)Follow the 50/72 rule for formatting
git add forgotten-file.jsStage the forgotten file
git commit --amend --no-editAdd to last commit without changing message
git commit --amend -m "Corrected message"Replace last commit with new message
git commit -a -m "Update config files"Auto-stage modified/deleted tracked files and commit
git add .Stage changes
git commit -S -m "Verified security patch"Create GPG-signed commit
Editor & Interactive Keys
Keyboard shortcuts when Git opens an editor for your commit message, or when using interactive modes.
Vim (default on many systems)
core.editor = vimNano
core.editor = nanoVS Code
core.editor = "code --wait"Patch Mode (-p)
git commit -pTroubleshooting
no changes added to commit (use "git add" and/or "git commit -a")
Cause: Files changed but nothing is staged - you forgot to run 'git add'. (If you see 'nothing to commit, working tree clean', there are no changes at all.)
Stage changes with 'git add' before committing. Use 'git status' to see what needs staging.
git add . && git commit -m "message"Please tell me who you are (Author identity unknown)
Cause: Git requires user.name and user.email to be configured before your first commit.
Configure your identity globally or for this repository.
git config --global user.name "Your Name" && git config --global user.email "you@example.com"Aborting commit due to empty commit message
Cause: You closed the editor without writing a message, or the message contained only comments.
Write a commit message in the editor, or use '-m' flag for inline message.
git commit -m "Your message here"pre-commit hook failed
Cause: A pre-commit hook (linting, tests, formatting) found issues and blocked the commit.
Fix the issues reported by the hook. Only bypass with '--no-verify' if absolutely necessary.
git commit --no-verify -m "message"commit-msg hook failed
Cause: Your commit message doesn't match the format required by project conventions.
Follow your project's commit message convention (e.g., Conventional Commits format).
git commit -m "feat: add new feature"Changes not staged for commit
Cause: You have modified files but haven't added them to the staging area.
Use 'git add' to stage specific files, or 'git commit -a' to auto-stage tracked files.
git add <file> && git commit -m "message"error: failed to push some refs (after amend)
Cause: You amended a commit that was already pushed, creating divergent history.
Either force push (if you're the only one on the branch) or revert and create a new commit.
git push --force-with-leaseYour branch is ahead of 'origin/main' by X commits
Cause: You have local commits that haven't been pushed yet.
This is normal! Push when ready, or continue working locally.
git push origin mainTips & Pitfalls
Best Practices
- ✓Write clear commit messages: Start with a verb (Add, Fix, Update, Remove) and keep the subject under 50 characters.
- ✓Use imperative mood: Write 'Add feature' not 'Added feature' - imagine completing 'This commit will...'
- ✓Make atomic commits: Each commit should represent one logical change. Don't mix bug fixes with new features.
- ✓Review before committing: Use 'git diff --staged' to see exactly what you're about to commit.
- ✓Never amend pushed commits: Amending rewrites history. Only amend commits that haven't been shared.
- ✓Separate subject from body with a blank line: For longer messages, the first line is the subject, then a blank line, then details.
- ✓Wrap body text at 72 characters: This ensures readability in terminals and git log output.
- ✓Reference issue numbers: Include 'Fixes #123' or 'Closes #456' to link commits to issue tracking.
- ✓Commit often, push less often: Small, frequent commits are easier to review and revert than large ones.
- ✓Don't commit generated files: Keep build artifacts, dependencies (node_modules), and compiled files out of commits.
Common Mistakes
- ✗Running 'git commit' without staging changes first: You must use 'git add' first, unless using 'git commit -a' which only stages tracked files.
- ✗Writing vague commit messages like 'fix' or 'update': Use clear, descriptive messages that explain WHY the change was made.
- ✗Committing too many unrelated changes at once: Make atomic commits - one logical change per commit for easier review and revert.
- ✗Using --amend on commits already pushed to shared branches: Amending rewrites history and will cause problems for others.
- ✗Forgetting to configure user.name and user.email: Git requires author identity before your first commit.
FAQ
'git commit' only commits what's in the staging area. 'git commit -a' automatically stages all modified and deleted tracked files before committing - but NOT new untracked files. You still need 'git add' for new files.
For unpushed commits: 'git reset --soft HEAD~1' keeps changes staged, 'git reset HEAD~1' (same as --mixed) unstages them, 'git reset --hard HEAD~1' discards them. For pushed commits: use 'git revert HEAD' to create a new commit that undoes the changes.
For the last commit: 'git commit --amend -m "New message"'. For older commits: use 'git rebase -i' and change 'pick' to 'reword'. Never do this for commits already pushed to shared branches.
Git opens your configured editor to write a message. If you close without saving a message, the commit is aborted. Use '-m' for inline messages or '--allow-empty-message' to commit without one (not recommended).
Stage the additional files with 'git add <file>', then run 'git commit --amend --no-edit'. This adds them to the previous commit without changing its message.
'--amend' replaces the last commit entirely (new hash!). Use it to fix typos in messages or add forgotten files. Never use it on commits you've already pushed to shared branches - it rewrites history.
Use multiple -m flags: 'git commit -m "Subject" -m "Body paragraph"'. Each -m creates a new paragraph. Or just use 'git commit' to open your editor.
Keep the subject line under 50 characters (hard limit 72). Wrap the body at 72 characters. This ensures readability in terminal output, git log, and GitHub/GitLab interfaces.
Use 'git commit --no-verify' or '-n'. This bypasses pre-commit and commit-msg hooks. Use sparingly - hooks exist to maintain code quality.
Yes, use 'git commit --allow-empty -m "message"'. Useful for triggering CI/CD pipelines or marking milestones in history without code changes.