Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Staging and Committing

git commit command

Reference for git commit: record staged changes as a commit, write messages with -m and -F, use -a, --allow-empty, --fixup, --author, --date, trailers and signing.

git commit creates a new commit from the contents of the index. A commit stores a snapshot of the whole project, the author and committer identities with timestamps, a message, and the ID of its parent commit (two or more for a merge). The current branch then moves to the new commit.

Only what is staged goes in. Files you changed but did not stage stay as they are in the working tree, unless you pass -a (which stages tracked modifications first) or give explicit paths.

Terminal
git commit [-a] [-m <msg> | -F <file> | -C <commit>] [<options>] [-- <pathspec>...]

Try it

Bash Staged versus -a, messages and the commit object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Empty commits, authors, dates, trailers and fixups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git commit -m "<subject>"
git commit -a -m "<subject>"
git commit --amend            # see the git commit --amend page
git commit <pathspec>... -m "<subject>"
Option Description
-m <msg> Use this message; repeat the option for separate paragraphs.
-F <file> Read the message from a file, or - for standard input.
-C <commit>, -c <commit> Reuse the message (and authorship) of another commit; -c opens the editor first.
-a, --all Stage all modified and deleted tracked files before committing. New files are not included.
-p, --patch Choose hunks interactively, like git add -p.
--allow-empty Create a commit even if it changes nothing.
--allow-empty-message Permit an empty message.
--amend Replace the last commit, see git commit --amend.
--fixup=<commit>, --squash=<commit> Create a commit to be merged into the target later by git rebase -i --autosquash.
--author=<name <email>> Override the author (the committer stays you).
--date=<date> Override the author date.
--reset-author With -C/-c/--amend, use you and now as author.
-s, --signoff Append a Signed-off-by: trailer.
--trailer <token:value> Append a trailer line.
-S[<keyid>], --gpg-sign Sign the commit. --no-gpg-sign overrides commit.gpgSign.
-n, --no-verify Skip the pre-commit and commit-msg hooks.
-e, --edit / --no-edit Force or skip the editor.
-v, --verbose Show the diff in the editor template (twice for the unstaged diff too).
-q, -s, --dry-run, --short, --porcelain Preview what would be committed.
--cleanup=<mode> strip, whitespace, verbatim, scissors, default.
-u<mode> How to treat untracked files in the editor template.
-t <file>, --template=<file> Message template (config: commit.template).
--no-post-rewrite Skip the post-rewrite hook.

Exit status: 0 when a commit was created; 1 when there was nothing to commit (nothing to commit, working tree clean) or a hook failed; nonzero when no identity can be found.

Usage notes

  • Message format. A short subject (about 50 characters), a blank line, then a body wrapped near 72 columns. Lines starting with # are comments in an editor session and are removed. Many tools show only the subject, so lead with the reason for the change.
  • -a is not add -A. It includes modified and deleted tracked files only. A new file still needs git add.
  • Pathspec form. git commit file -m msg commits the current content of that file (from the working tree) and ignores other staged changes ("only" mode); this surprises people. Prefer staging explicitly.
  • Hooks run in order: pre-commit, prepare-commit-msg, commit-msg, then post-commit. A non-zero exit from the first three aborts; --no-verify skips pre-commit and commit-msg.
  • Identity. Author and committer come from GIT_AUTHOR_*/GIT_COMMITTER_* or user.name and user.email. Check with git var.
  • Merging. During a merge, git commit without -m concludes it with the prepared message; during a conflict it refuses until all files are resolved and staged.
  • Empty commits are legitimate for markers and CI triggers, but pushing many is noise. During rebases, empty commits are dropped unless you ask otherwise.
  • Commits are immutable. What you can change later (message, content, parents) produces a new commit; see amend and rebase. Never rewrite commits others already pulled.