Skip to content
masterclass Buddy

Git · Command

git commit

git commit records a new snapshot in the repository from the current contents of the index, together with a log message. The new commit becomes a child of HEAD, and the current branch moves forward to point at it.

Last updated

How it works

Git builds a tree from the index (the staging area) and writes a commit object whose parent is HEAD. It then moves the current branch to the new commit. If no branch is checked out, HEAD is detached and moves on its own. You choose what goes into the commit by staging with git add or git rm beforehand, by using -a to stage all tracked changes automatically, by naming paths on the command line, or by picking hunks with --patch. The message comes from -m, -F, -C or -c, or from an editor. A cleanup mode trims the message, and hooks such as pre-commit and commit-msg can check or reject the commit.

Syntax

git commit [-a | --interactive | --patch] [-s] [-v] [-u[<mode>]] [--amend] [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>] [-F <file> | -m <msg>] [--reset-author] [--allow-empty] [--allow-empty-message] [--no-verify] [-e] [--author=<author>] [--date=<date>] [--cleanup=<mode>] [--[no-]status] [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]] [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]] [--] [<pathspec>...]

Parameters

NameTypeDescription
-a, --allflagStages modified and deleted tracked files before committing. Untracked files are left out.
-m <msg>, --message=<msg>stringSets the commit message. If you repeat it, each value becomes its own paragraph. It cannot be combined with -c, -C or -F.
-F <file>, --file=<file>pathReads the message from a file. Use - to read it from standard input.
--amendflagReplaces the tip of the current branch with a new commit that keeps the same parents and author.
--fixup=[(amend|reword):]<commit>commitCreates a fixup! or amend! commit for git rebase --autosquash to fold into <commit> later.
-p, --patchflagLets you choose the hunks to commit interactively.
-s, --signoffflagAdds a Signed-off-by trailer for the committer.
-S[<key-id>], --gpg-signstring (optional)GPG-signs the commit. Without a key ID it uses the committer identity.
-n, --no-verifyflagSkips the pre-commit and commit-msg hooks.
--dry-runflagShows what would be committed without creating a commit.
<pathspec>...pathspecCommits the current working-tree contents of the matching files and leaves other staged changes out of this commit.

Returns

Creates a new commit object and moves the current branch (or a detached HEAD) to it. Unless -q is given, it prints a summary of the commit.

Key points

  • The -a shortcut only covers files Git already tracks. A brand-new file still needs git add before any commit will include it.
  • If you name paths on the command line, Git commits only those files as they are in your working tree. Changes staged for other files stay staged for a later commit.
  • You can pass -m more than once to build a message with several paragraphs, for example a short title followed by a longer body.
  • --dry-run lets you check what a commit would contain, given the same options and paths, before you actually make it.
  • Author and committer identity comes from GIT_AUTHOR_* and GIT_COMMITTER_* environment variables first. If those are not set, Git falls back to user.name and user.email in config.
  • With -v, the editor shows the diff being committed so you can write an accurate message. The diff is stripped out when the commit is saved.
  • --fixup=<commit> creates a follow-up commit that changes another commit's content but keeps its message. git rebase --autosquash squashes it into that commit later.
  • --signoff adds a Signed-off-by trailer. What that trailer means, such as agreeing to a DCO, is up to each project.

Common mistakes

  • --amend rewrites history. If you amend a commit that has already been pushed, collaborators who built on the old commit will have to recover.
  • While you are finishing a conflicted merge, git commit refuses to run if you give it pathnames.
  • If you use a message template and close the editor without changing anything, Git aborts the commit instead of saving the template as the message.
  • If a commit fails, the message you typed is saved in .git/COMMIT_EDITMSG. Copy it out before running git commit again, because the next run overwrites it.

Example: Stage, commit, then fix up the last commit

#!/usr/bin/env bash
set -euo pipefail

git init -q demo && cd demo
git config user.name "Ada Lovelace"
git config user.email "ada@example.com"

echo "hello" > hello.txt
git add hello.txt

# Preview, then commit with a title and a body paragraph
git commit --dry-run --short
git commit -m "Add greeting file" -m "Initial content for the demo."

# Edit a tracked file and commit it without git add
echo "hello, world" > hello.txt
git commit -a -m "Expand greeting"

# Forgot a file? Stage it and fold it into the last commit
echo "notes" > NOTES.md
git add NOTES.md
git commit --amend --no-edit

git log --oneline

The script stages and previews a first commit, commits a tracked-file change with -a, and then folds a forgotten file into the latest commit with --amend --no-edit, which keeps the original message.

When to use it

Everyday snapshot

After staging related edits with git add, record them as one focused commit with a clear title line.

Correcting the latest local commit

You spot a typo or a missing file just after committing and haven't pushed yet. Use --amend to replace the commit.

Preparing autosquash cleanups

During code review, use --fixup=<sha> to make commits that git rebase -i --autosquash will merge into the right earlier commits.

Concluding a merge

Once the conflicts are resolved and staged, run git commit to record the merge commit using the message Git has pre-filled.

Interview questions

QWhat exactly does git commit put into the new commit, and how does -a change that?

By default it records the contents of the index, not the working tree, so unstaged edits are left out. With -a, Git first stages every change to tracked files, including deletions, and then commits. Untracked files are never added by -a.

Follow-up: What happens with git commit file.c when other files are already staged? Only file.c's current working-tree contents are committed. The other staged changes are held back and still staged for the next commit.

QHow does git commit --amend work, and when should you avoid it?

It replaces the tip of the current branch with a new commit. The tree is rebuilt as usual, the old message is the starting point, and the parents and author stay the same. Because the commit ID changes, it rewrites history, so avoid it on commits others may already have.

Follow-up: How do you amend only the message while ignoring what is currently staged? Use git commit --amend --only, which needs no paths and leaves the staged changes out. To change authorship too, add --reset-author.

QHow can a team enforce checks or metadata on commits?

git commit runs the pre-commit, prepare-commit-msg, commit-msg and post-commit hooks, and any of them can validate or reject a commit. Policy metadata can be added with --signoff, --trailer and -S for GPG signing. A commit.template file can guide contributors on how to write messages.

Follow-up: Can a developer skip those hooks? Yes. -n or --no-verify skips the pre-commit and commit-msg hooks, so important checks also need to run on the server side or in CI.

  • git add
  • git rm
  • git status
  • git restore
  • git reset
  • git rebase
  • git merge
  • git commit-tree

Source: Git - git-commit Documentation. Checked 2026-11-02.