git commit

Record the index, git commit -a, empty-commit refusal, and amend in a temporary repository, checked on Git 2.39.5.

命令页正文目前为英文。

git commit records a new snapshot from the index, not from unstaged working-tree edits, and not from untracked files. The new commit becomes HEAD, and the current branch name moves with it.

git commit --amend replaces the current HEAD commit with a new object. If that commit was already pushed or shared, rewriting it leaves other copies of the old SHA in place and usually requires a history recovery. The amend example runs only in a temporary repository. Do not treat amend as a casual edit of published history.

Examples were checked against Git 2.39.5. See the official documentation for the complete command.

Git command catalog → · Git tutorial overview →

The index, -a, and nothing to commit

A plain git commit records only what is staged. In the example, staged.txt is committed, the edit in tracked.txt stays unstaged, and loose.txt stays untracked.

git commit -a stages modifications and deletions of already tracked files, then commits. It still leaves loose.txt untracked. A later commit with only that untracked file present exits 1 and prints that nothing was added to the commit. Review git status before you commit; -a is not a shortcut for "commit everything in the directory".

git commit -m, git commit -a

git commit -m <message>
git commit -a -m <message>

Record the index as a commit, or auto-stage tracked modifications first, and refuse when there is nothing to record.

ParameterDescription
-m <message>The commit message. The example uses --quiet so the script can print its own SHA and path names.
-a / --allStage modifications and deletions of tracked files before committing. Untracked files are not added.

Returns: On success, HEAD is a new commit whose tree matches the recorded index.

Exit status

  • 0 — A new commit was written. Measured for the staged file and for -a on Git 2.39.5.
  • 1 — Nothing to commit. In the example an untracked file was present, and Git printed that nothing was added to the commit.
  • The example greps the stable "nothing added to commit..." line from Git's status-style output, instead of repeating the full hint block.
bash
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'one\n' > tracked.txt
git add tracked.txt
git commit -m "Add tracked" >/dev/null
printf 'one\ntwo\n' > tracked.txt
printf 'staged\n' > staged.txt
printf 'loose\n' > loose.txt
git add staged.txt
echo "before"
git status --porcelain
echo "commit-staged"
git commit --quiet -m "Add staged"
echo "commit-staged-exit=$?"
echo "after-staged"
git status --porcelain
echo "names-staged"
git show --name-only --pretty=format:%s HEAD
echo
echo "commit-all"
git commit -a --quiet -m "Update tracked"
echo "commit-a-exit=$?"
echo "after-all"
git status --porcelain
echo "names-all"
git show --name-only --pretty=format:%s HEAD
echo
echo "empty"
git commit -m "Nothing" >empty-out.txt 2>&1
echo "empty-exit=$?"
grep -F "nothing added to commit but untracked files present" empty-out.txt

Expected output

before
A  staged.txt
 M tracked.txt
?? loose.txt
commit-staged
commit-staged-exit=0
after-staged
 M tracked.txt
?? loose.txt
names-staged
Add staged
staged.txt

commit-all
commit-a-exit=0
after-all
?? loose.txt
names-all
Update tracked
tracked.txt

empty
empty-exit=1
nothing added to commit but untracked files present (use "git add" to track)

Related: git add, git status, git show

Official documentation →

Amend only in unpublished history

git commit --amend -m replaces the message of HEAD and writes a new SHA. The parent and, in the first amend, the tree stay the same, but the commit object is new. git commit --amend --no-edit keeps the message and records whatever is now in the index, including a newly staged edit.

Both steps are shown only in this temporary repository. If HEAD had already been pushed, other clones would still have the old SHA, and updating them would mean rewriting published history. This page does not push, force-push, or amend a real project commit.

git commit --amend, git commit --amend --no-edit

git commit --amend -m <message>
git commit --amend --no-edit

Replace HEAD with a new commit that has a new message or a new tree, only when that history is not already shared.

ParameterDescription
--amendBuild a replacement for the current HEAD commit instead of adding a new one on top of it.
--no-editKeep the existing message and amend the tree (and other recorded metadata) only.

Returns: HEAD moves to a new commit SHA. The previous HEAD is no longer named by the branch, though a reflog entry may still point at it in a normal repository.

  • The example uses --quiet and prints short SHAs so the rewrite is obvious without Git's default commit banner.
bash
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'one\n' > file.txt
git add file.txt
git commit --quiet -m "Add file"
before=$(git rev-parse --short HEAD)
echo "before=$before"
echo "subject=$(git log -1 --pretty=format:%s)"
git commit --amend --quiet -m "Add the first file"
echo "amend-exit=$?"
after=$(git rev-parse --short HEAD)
echo "after=$after"
echo "same-sha=$( [ "$before" = "$after" ] && echo yes || echo no )"
echo "subject=$(git log -1 --pretty=format:%s)"
printf 'two\n' >> file.txt
git add file.txt
git commit --amend --no-edit --quiet
echo "amend-no-edit-exit=$?"
echo "after-no-edit=$(git rev-parse --short HEAD)"
echo "subject=$(git log -1 --pretty=format:%s)"
echo "names"
git show --name-only --pretty=format:%s HEAD
echo

Expected output

before=ae79adc
subject=Add file
amend-exit=0
after=fbf4b5b
same-sha=no
subject=Add the first file
amend-no-edit-exit=0
after-no-edit=a57912c
subject=Add the first file
names
Add the first file
file.txt

Official documentation →