Committing Changes (git commit)
git commit takes whatever is in the staging area and turns it into a permanent snapshot in the repository — a commit. Each commit records the snapshot itself, your name and email, a timestamp, the commit message you wrote, and a pointer to the previous commit (its parent). Commits are the unit of history; every other Git feature is built on them.
The basic commit
The most common form
# Write the message inline git commit -m "Add login form" # Or omit -m and your editor opens for a longer message git commit
What a commit records
The tree — a snapshot of every tracked file at this moment (technically, a tree object that points to blobs).
Parent commit(s) — usually one. Merge commits have two parents.
Author — who originally wrote the change (name + email + timestamp).
Committer — who actually created the commit (usually the same person as the author).
Commit message — the human-readable description you typed.
A unique SHA-1 hash — derived from all of the above. Any change to any field changes the hash.
Multi-line messages without -m
If you run git commit with no -m, Git opens your configured editor with a template like this:
The commit message buffer
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch main # Changes to be committed: # modified: src/app.js # new file: src/header.js #
Write a subject line, leave a blank line, then write a longer explanation. Save and close. Your message is recorded with the commit.
The handy combos
Stage + commit tracked files in one step
git commit -am "Quick fix" # Equivalent to: git add -u && git commit -m "Quick fix" # Note: -a does NOT add untracked (new) files
Stage and commit only a specific file
git commit src/util.js -m "Tidy up util" # Stages and commits just that file; other changes are untouched
Amending the last commit
If you just made a commit and realised you forgot a file or mistyped the message, --amend replaces the last commit in-place:
Two common amend recipes
# Fix the commit message only git commit --amend -m "Correct message" # Add forgotten files to the last commit, keep the message git add forgotten.js git commit --amend --no-edit
Empty commits (with intent)
Useful for triggering CI
git commit --allow-empty -m "Re-trigger CI build"
Signing commits
GPG-signed commit
git commit -S -m "Signed commit" # Configure once so all commits are signed: git config --global commit.gpgsign true git config --global user.signingkey YOURKEYID
Setting an author different from yourself
Pair-programming attribution
git commit --author="Alice Smith <alice@example.com>" -m "Pair work" # Modern style: co-authors as a trailer (GitHub recognises this) git commit -m "Pair work" -m "Co-authored-by: Alice Smith <alice@example.com>"
Inspecting your commit
git log -1 # the most recent commit git show # full diff of the last commit git log --stat -1 # file change stats git log -1 --pretty=fuller # all metadata
What if I want to abort a commit?
When the editor opens for a commit message, save an empty file (or just close without saving) — Git will abort the commit cleanly.
On the command line,
git commit -m ""is rejected — empty messages are not allowed.After making a commit accidentally:
git reset --soft HEAD~1undoes it but keeps the changes staged.
Reverting vs amending vs resetting
--amend— change the last commit in place (rewrites history).git revert <hash>— create a new commit that undoes an old one. Safe on shared branches.git reset <hash>— move the branch pointer to an earlier commit. Powerful and a little dangerous.