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.
| Parameter | Description |
|---|---|
-m <message> | The commit message. The example uses --quiet so the script can print its own SHA and path names. |
-a / --all | Stage 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.
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