git commit is the moment when "edits" become "history". It takes a
snapshot of the index (not the working tree) and packages it into a
commit object.
What happens during a commit
- A tree object is assembled from the index (via
write-tree). - A commit object is created: tree + parent (HEAD) + author + committer + message. Author is who wrote the changes; committer is who recorded them. They usually match, but after a rebase, cherry-pick, or applied patch the committer is updated while the author stays the same.
- The current branch moves to the new commit SHA.
- HEAD follows the branch automatically.
See chapter 4 of the textbook (plumbing) for a detailed walkthrough.
Basic forms
git commit # opens editor for the message
git commit -m "fix typo" # short inline message
git commit -m "title" -m "body" # title + blank line + body
git commit -a # auto-stage all tracked files
# (equivalent to git add -u + commit)
git commit --amend # rewrite the last commit
--amend
The most common special mode. Does not create a new commit; it rewrites the HEAD commit. Use it to:
- add a forgotten file (
git add forgot.js && git commit --amend) - fix the message (
git commit --amend -m "new message") - keep the message but update the content (
git commit --amend --no-edit)
After --amend the old commit remains in .git/objects/ as a dangling
object. git reflog will see it for about a month.
Important: if the commit was already pushed, --amend creates a
conflict on the next push. Either use push --force-with-lease, or create
a new commit instead of amending.
Commit message
A convention that originated in the Linux kernel and is now widely adopted:
area: short description in imperative mood
More detail, if needed. What and why,
not how. The code explains how. Wrap at 72 characters.
Closes #123
First line at most 50 characters, in imperative mood (add, fix, not
added or adds). Blank line. Body only when you need to explain the
motivation or warn about subtleties.
Pitfalls
git commitcommits the index, not the working tree. If you forgotgit add, the changes will not be included.git commit -am "..."(via-a) auto-stages only modified tracked files. New untracked files are excluded. Those always need an explicitgit add.- An empty commit fails without a flag. If you need one (for example, to
mark a release), use
git commit --allow-empty -m "release v1.0".