Everyday snapshot
After staging related edits with git add, record them as one focused commit with a clear title line.
Git · Command
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
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.
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>...]| Name | Type | Description |
|---|---|---|
-a, --all | flag | Stages modified and deleted tracked files before committing. Untracked files are left out. |
-m <msg>, --message=<msg> | string | Sets 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> | path | Reads the message from a file. Use - to read it from standard input. |
--amend | flag | Replaces the tip of the current branch with a new commit that keeps the same parents and author. |
--fixup=[(amend|reword):]<commit> | commit | Creates a fixup! or amend! commit for git rebase --autosquash to fold into <commit> later. |
-p, --patch | flag | Lets you choose the hunks to commit interactively. |
-s, --signoff | flag | Adds a Signed-off-by trailer for the committer. |
-S[<key-id>], --gpg-sign | string (optional) | GPG-signs the commit. Without a key ID it uses the committer identity. |
-n, --no-verify | flag | Skips the pre-commit and commit-msg hooks. |
--dry-run | flag | Shows what would be committed without creating a commit. |
<pathspec>... | pathspec | Commits the current working-tree contents of the matching files and leaves other staged changes out of this commit. |
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.
#!/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 --onelineThe 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.
After staging related edits with git add, record them as one focused commit with a clear title line.
You spot a typo or a missing file just after committing and haven't pushed yet. Use --amend to replace the commit.
During code review, use --fixup=<sha> to make commits that git rebase -i --autosquash will merge into the right earlier commits.
Once the conflicts are resolved and staged, run git commit to record the merge commit using the message Git has pre-filled.
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.
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.
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 addgit rmgit statusgit restoregit resetgit rebasegit mergegit commit-tree