git commit

Basic Snapshotting

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>...]
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (8)

Use the given message as the commit message. Can be used multiple times for multi-paragraph messages.

RecommendedFine for short messages; use the editor for a subject plus body

Example

git commit -m "Add login feature"

Automatically stage all modified and deleted tracked files before committing. Does NOT add new untracked files.

When neededSkips the staging review, and never includes new untracked files

Example

git commit -a -m "Update all config files"

Replace the tip of the current branch with a new commit. Use to fix the last commit's message or add forgotten files. Creates a new commit hash.

When neededSafe on unpushed commits; rewrites history if already pushed

Example

git commit --amend -m "Fixed message"

Show the diff of staged changes in the commit message template. Helps you review what you're committing while writing the message.

RecommendedShows the diff while you write the message

Example

git commit -v

Show what would be committed without actually creating a commit. Great for verifying before committing.

When neededPreviews what would be committed without committing

Example

git commit --dry-run

Suppress the commit summary message that shows files changed and insertions/deletions.

When neededSuppresses the commit summary — mainly for scripts

Example

git commit -q -m "Silent commit"

Open the editor for the commit message, even when using -m, -F, or -C. Allows you to further edit the message.

When neededOpens the editor even when -m, -F or -C already gave a message

Example

git commit -m "Initial message" -e

Use the selected commit message without launching an editor. Commonly used with --amend to add files without changing the message.

When neededReuses the existing message, typically together with --amend

Example

git commit --amend --no-edit

Intermediate (10)

Read the commit message from the given file. Use '-' to read from standard input.

When neededTakes the message from a file or stdin — for scripts and long messages

Example

git commit -F commit-message.txt

Start the commit-message editor with the contents of the given file (commit.template sets this by default). Leaving the template unchanged aborts the commit; it has no effect when -m or -F provides the message.

When neededStarts from a team commit-message template

Example

git commit -t .gitmessage

Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.

When neededReuses another commit's message and authorship as-is

Example

git commit -C HEAD~1

Like -C, but opens the editor to let you edit the message. Reuses authorship info but lets you modify the text.

When neededStarts from another commit's message and lets you edit it

Example

git commit -c HEAD~1

Stage the given paths in addition to what is already staged, then commit everything. The official docs note this is usually not what you want unless you are concluding a conflicted merge.

When neededStages the given paths in addition to what is already staged

Example

git commit -i extra-file.js -m "Include additional file"

Commit only the working-tree content of the specified files; other staged changes stay staged for later. Default when paths are given.

When neededCommits only the given paths — already the default when paths are given

Example

git commit -o urgent-fix.js -m "Hotfix"

Use the interactive patch selection interface to choose which changes to commit. Allows you to commit parts of files.

RecommendedPick hunks and commit them in one step

Example

git commit -p

Add a Signed-off-by trailer at the end of the commit message. Used to certify you have rights to submit the code (Developer Certificate of Origin).

When neededAdd it when the project requires a DCO sign-off

Example

git commit -s -m "Add feature"

Disable adding the Signed-off-by trailer. Countermands an earlier --signoff on the command line (e.g. from an alias).

When neededCancels an earlier --signoff on the same command line

Example

git commit --no-signoff -m "Skip signoff"

Include the output of git status in the commit message template shown in the editor. On by default, but useful to override --no-status.

When neededIncludes the status list in the editor template (the default)

Example

git commit --status

Advanced (12)

Override the commit author. Specify using the 'Author Name <email@example.com>' format.

When neededCredits someone else's work; prefer a Co-authored-by trailer for pairing

Example

git commit --author="John Doe <john@example.com>" -m "Fix bug"

Override the author date used in the commit. Accepts various formats like ISO 8601, RFC 2822, or relative dates.

When neededOverrides the author date; rarely needed

Example

git commit --date="2025-01-15 10:30:00" -m "Backdated commit"

With --amend, -C or -c (or when committing after a conflicting cherry-pick), make you — the committer — the author of the resulting commit and renew the author timestamp.

When neededClaims authorship when amending or reusing a message

Example

git commit --amend --reset-author

Bypass the pre-commit and commit-msg hooks. Use with caution as these hooks often enforce code quality.

Not recommendedSkips the pre-commit and commit-msg checks — fix the hook instead

Example

git commit -n -m "Quick fix (skip hooks)"

Allow creating a commit with no changes. Useful for triggering CI/CD pipelines or marking milestones.

When neededCommits with no changes, e.g. to trigger CI

Example

git commit --allow-empty -m "Trigger rebuild"

Allow creating a commit with an empty message. Generally not recommended as it makes history harder to understand.

Not recommendedEmpty messages make history unreadable

Example

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

Specify how the commit message should be cleaned up before committing. Modes: strip, whitespace, verbatim, scissors, default (strip if the editor opens, else whitespace).

When neededControls how # lines and whitespace are stripped from the message

Example

git commit --cleanup=verbatim -m "Keep message exactly as-is"

GPG-sign the commit. Proves the commit was made by you and hasn't been tampered with. Requires GPG setup.

When neededSign when your project or host verifies signatures

Example

git commit -S -m "Signed commit"

Do not GPG-sign the commit. Overrides commit.gpgSign configuration if it's set to true.

When neededOverrides commit.gpgSign for one commit

Example

git commit --no-gpg-sign -m "Unsigned commit"

Create a fixup commit for use with git rebase --autosquash. The commit will be automatically squashed into the specified commit during rebase.

RecommendedClean way to fix an older commit together with rebase --autosquash

Example

git commit --fixup=abc1234

Create a squash commit for use with git rebase --autosquash. Like --fixup, but preserves both commit messages for combining during rebase.

When neededLike --fixup but lets you combine the messages

Example

git commit --squash=abc1234

Add a trailer (key: value) to the commit message. Common trailers include Reviewed-by, Co-authored-by, Fixes, etc.

When neededStructured metadata such as Co-authored-by or Reviewed-by

Example

git commit --trailer "Reviewed-by: Jane <jane@example.com>" -m "Fix bug"

Expert (8)

When doing a dry-run, give the output in short format. Shows file status in a compact format, like git status -s. Implies --dry-run.

When neededDry-run output in short status format

Example

git commit --dry-run --short

When doing a dry-run, give the output in long format (default). Shows detailed file status information.

When neededDry-run output in long status format

Example

git commit --dry-run --long

When doing a dry-run, give the output in porcelain-ready format. Stable, script-friendly output that won't change between Git versions.

When neededMachine-readable dry-run output for scripts

Example

git commit --dry-run --porcelain

Show the branch and tracking info even in short-format during dry-run.

When neededAdds branch info to the short/porcelain dry-run output

Example

git commit --dry-run --short --branch

When showing short or porcelain status output, terminate entries with NUL instead of newline. Useful for parsing filenames with special characters.

When neededNUL-terminated dry-run output for scripts

Example

git commit --dry-run --porcelain -z

Do not include the output of git status in the commit message template when using an editor.

When neededHides the status list in the editor template

Example

git commit --no-status

Bypass the post-rewrite hook, which git commit runs only when amending (--amend).

When neededSkips the post-rewrite hook on --amend; rarely needed

Example

git commit --amend --no-post-rewrite

Read pathspec from a file (one per line) instead of from the command line. Use '-' to read from standard input.

When neededFor scripts committing many paths

Example

git commit --pathspec-from-file=files.txt -m "Commit listed files"

Choosing the Right Option

Message Options

Different ways to provide or edit the commit message

CommandSourceOpens EditorUse Case
git commit -m "msg"Inline✗Quick commits with short messages
git commit -F fileFile✗Pre-written message from file
git commit -t fileTemplate✓Start with a template in editor
git commit -eEditor✓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

CommandModeStrips CommentsStrips WhitespaceBest For
--cleanup=stripstrip✓✓Most commits
--cleanup=whitespaceWhitespace✗✓Keep # comments
--cleanup=verbatimVerbatim✗✗Exact message preservation
--cleanup=scissorsScissorsOnly below scissors line✓Keep # lines when using the editor

Author & Date

Override who made the commit and when

CommandOverridesFormatTypical Use
--author="Name <email>"AuthorName <email>Committing on someone's behalf
--date="date"Author dateISO 8601 / relativeBackdating commits
--reset-authorBothCurrent user/timeTaking ownership of applied patch

Amending & Fixing History

Options for modifying existing commits

CommandCreatesModifies HEADUse With
git commit --amendReplacement✓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

CommandStages FirstCommitsWorking Dir
git commit✗Staged onlyUnchanged
git commit -aTracked filesAll trackedUnchanged
git commit -i <file>SpecifiedStaged + fileUnchanged
git commit -o <file>Specified onlyOnly fileOther staged excluded
git commit -pInteractiveSelected hunksUnchanged

GPG Signing

Cryptographically sign commits for verification

CommandActionKey UsedWhen to Use
git commit -SSignDefault GPG keyProve commit authenticity
git commit -S<keyid>SignSpecific keyUse alternate signing key
git commit --no-gpg-signDon't signNoneOverride commit.gpgSign=true

Hook Control

Bypass or control pre-commit and post-commit hooks

CommandSkipsRisk LevelWhen to Use
git commit --no-verifypre-commit + commit-msgMediumEmergency fix, fix hooks later
git commit --no-post-rewritepost-rewriteLowSkip slow post-amend processing

Dry-Run & Output Format

Preview what would be committed without actually committing

CommandFormatParseableShows
--dry-runLong (default)✗Full status with hints
--dry-run --shortShort✓Compact status codes
--dry-run --porcelainPorcelain✓Stable format for scripts
--dry-run --branchWith branch✓Adds tracking info to short
--dry-run -zNUL-separated✓Safe for special filenames

Editor Status Display

Control what appears in the editor when writing commit message

CommandIn EditorEffect
git commit --statusStatus as commentsSee files being committed (default)
git commit --no-statusNo file listCleaner editor, less distraction
git commit -vFull diffReview exact changes while writing
git commit -qNormal editorSuppress summary after commit

Special Commits

Create commits in unusual situations

CommandAllowsCreates CommitUse Case
--allow-emptyNo changes✓CI triggers, branch markers
--allow-empty-messageNo message✓Automated scripts (not recommended)
--trailer "Key: value"Metadata✓Co-authored-by, Fixes, Reviewed-by

Workflow Examples

1
git add .

Stage your changes

2
git commit -m "Fix typo in README"

Commit with a short message

1
git add -A

Stage all changes

2
git commit

Opens editor for multi-line message

3
(Write: subject line, blank line, detailed body)

Follow the 50/72 rule for formatting

1
git add forgotten-file.js

Stage the forgotten file

2
git commit --amend --no-edit

Add to last commit without changing message

1
git commit --amend -m "Corrected message"

Replace last commit with new message

1
git commit -a -m "Update config files"

Auto-stage modified/deleted tracked files and commit

1
git add .

Stage changes

2
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 = vim
iEnter insert mode (start typing)
EscExit insert mode
:wqSave and quit (complete commit)
:q!Quit without saving (abort commit)
ddDelete current line
uUndo last change

Nano

core.editor = nano
Ctrl+OSave file (write out)
Ctrl+XExit (prompts to save if changed)
Ctrl+KCut current line
Ctrl+UPaste cut text
Ctrl+WSearch in file

VS Code

core.editor = "code --wait"
Ctrl+SSave file
Ctrl+WClose tab (completes commit)
Ctrl+ZUndo
Close without savingAbort commit

Patch Mode (-p)

git commit -p
yStage this hunk
nDon't stage this hunk
qQuit; don't stage remaining hunks
aStage this and all remaining hunks
dDon't stage this or remaining hunks
sSplit hunk into smaller hunks
eManually edit hunk
?Show help

Troubleshooting

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-lease

Your 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 main

Tips & 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

A:

'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.

A:

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.

A:

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.

A:

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).

A:

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.

A:

'--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.

A:

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.

A:

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.

A:

Use 'git commit --no-verify' or '-n'. This bypasses pre-commit and commit-msg hooks. Use sparingly - hooks exist to maintain code quality.

A:

Yes, use 'git commit --allow-empty -m "message"'. Useful for triggering CI/CD pipelines or marking milestones in history without code changes.