Sub topic 14
The git commit -a shortcut and its limits
This chapter assumes you can create a repository with git init, stage files with git add, and read git status. It covers what git commit -a does, and where it stops working.
What -a does
The normal cycle is two steps: git add copies changes into the staging area, then git commit records the staging area. The -a flag (short for --all) combines them for files Git already tracks. Before committing, it stages every modification and deletion of a tracked file.
echo "Second line." >> README.md
git commit -a -m "Extend README"The first command edits README.md, which is already tracked. The second stages that edit and commits it in one step. You can also write git commit -am "Extend README", which combines the flags.
Where it stops
The shortcut has three limits.
- New files are ignored. A file Git has never tracked stays untracked. If you create
notes.txtand rungit commit -a, the commit leaves it out. Rungit add notes.txtfirst. - It takes everything tracked. Every modified tracked file goes into the commit. That includes edits you did not mean to include, such as a debugging line or an unrelated fix.
- It bypasses the staging area. Anything you carefully staged is committed along with all your other tracked changes. You lose the chance to build a focused commit.
Here is limit 1 in practice:
echo "Draft ideas" > notes.txt
echo "Third line." >> README.md
git commit -am "Update README"
git statusThe commit contains the README.md change only. git status still lists notes.txt under untracked files.
When to use it
The shortcut suits small, single-purpose changes. Examples are fixing a typo in one file or committing a quick edit when you have reviewed the diff and know it all belongs together.
Use the full git add and git commit cycle when you want control. That includes commits that mix new and modified files, and changes that should be split into separate commits. For splitting changes inside one file, git add -p is the better tool.
| Situation | Better choice |
|---|---|
| One small edit to a tracked file | `git commit -am "message"` |
| You created a new file | `git add file`, then `git commit` |
| Several unrelated edits in progress | Stage selected files, then `git commit` |
| Two changes in the same file | `git add -p`, then `git commit` |
Common errors
- "Nothing added to commit but untracked files present." You ran
git commit -awhen only new files had changed. Stage them withgit add. - A commit that is too large. If
-acaptured too much, undo the commit and redo it in smaller pieces. If you have not shared the commit,git reset --soft HEAD~1removes it and leaves the changes staged. Then rungit restore --staged <file>to unstage whatever should go in a later commit.
Exercise
You edited app.py, which is tracked, and created a new file config.py. You run git commit -am "Add config support". Which files are in the commit, and what do you need to do to include the other one?
Show answer
Only app.py is committed, because -a stages modified tracked files only. config.py is untracked, so run git add config.py and then either make a new commit or fix the last one with git commit --amend.