git commit — Saving Snapshots
The git commit command creates a new snapshot of your staged changes. Each commit is a permanent record in your project's history.
Anatomy of a Commit
Every commit contains:
- A unique SHA — A hash identifier (e.g.,
a1b2c3d) - Your changes — The staged files at the time of commit
- A message — A human-readable description
- Author & timestamp — Who made it and when
- Parent commit(s) — What came before (for history)
Writing Good Commit Messages
A good commit message explains why, not just what:
- ✅
"Fix login timeout by increasing session duration" - ✅
"Add user profile page with avatar upload" - ❌
"Fix bug" - ❌
"Update files"
Try it!
The repository is already initialized. Make two separate commits:
- Create a file:
echo "first" > file1.txt - Stage and commit:
git add file1.txtthengit commit -m "Add first file" - Create another file:
echo "second" > file2.txt - Stage and commit:
git add file2.txtthengit commit -m "Add second file" - Check the log:
git log --oneline
Watch the graph on the right update with each commit!
The -a Flag Shortcut
Once files are tracked (committed at least once), you can skip git add by using:
git commit -am "message"
This stages all modified tracked files and commits them in one step. But it won't add new untracked files.