git commit
Saving snapshots to history with git commit and writing good commit messages.
What you'll learn
- How to create a commit with git commit -m
- What makes a good commit message
- How to amend the last commit
- The relationship between staging and committing
Concept
What git commit Does
git commit takes everything in the staging area and saves it as a permanent snapshot in the repository history. Each commit gets a unique hash (like a1b2c3d) that identifies it forever.
git commit -m "Add user login page"
The -m flag lets you write the message inline. Without it, Git opens your text editor (usually Vim or Nano) for a longer message.
The Anatomy of a Commit
A commit records:
- A snapshot of all staged files at that moment
- The author name and email (from your config)
- A timestamp of when the commit was made
- The commit message describing the change
- A hash (SHA-1) that uniquely identifies this commit
- A pointer to the parent commit (forming the history chain)
Writing Good Commit Messages
A good commit message answers the question: "Why did I make this change?" The code itself shows what changed — the message should explain the reasoning.
Good messages:
Add user login page with OAuthFix crash when cart is emptyRefactor payment logic into separate moduleUpdate README with installation steps
Bad messages:
fix— what did you fix?changes— what changes?wip— what work in progress?asdf— meaningless
Conventional Commits
Many projects follow the Conventional Commits format, which prefixes messages with a type:
feat: add dark mode toggle
fix: correct total price calculation
docs: update API documentation
chore: upgrade dependencies
refactor: simplify auth flow
test: add unit tests for cart
This makes it easy to generate changelogs and automate version bumps.
Amending the Last Commit
If you made a typo in your commit message or forgot to stage a file, you can fix the last commit without making a new one:
# Fix only the message
git commit --amend -m "Add user login page with OAuth and tests"
# Add a forgotten file to the previous commit
git add forgotten-file.js
git commit --amend --no-edit
Warning: never amend a commit you have already pushed to a shared remote — it rewrites history and will cause problems for other developers.
Skipping the Staging Area
If you are committing all changes to tracked files (no new files), you can skip git add:
git commit -am "Update login styles"
The -a flag automatically stages modified (but not new) files before committing. It does not stage untracked files.
Example
# Stage and commit in two steps (the standard workflow)
git add README.md
git commit -m "Add project README with setup instructions"
# Stage everything and commit with a conventional commit message
git add .
git commit -m "feat: add user authentication with JWT"
# Commit all modified tracked files in one step (skips untracked)
git commit -am "fix: correct off-by-one error in pagination"
# Multi-line commit message (opens your editor if you omit -m)
git commit -m "Refactor payment processing
- Extract validation into its own function
- Add error handling for declined cards
- Update tests to cover new flow"
# Amend the last commit (fix a typo in the message)
git commit --amend -m "feat: add user authentication with JWT tokens"
# Add a forgotten file to the last commit without changing the message
git add .env.example
git commit --amend --no-edit
Shows the two-step add+commit, the -a shortcut for tracked files, a multi-line message, amending to fix a message, and amending to add a forgotten file. Note the Conventional Commits style (feat:, fix:).
Try it
- Code Diff
See what your commit will contain before committing.
- CI/CD YAML Generator
Commits trigger CI pipelines — generate one to see the connection.
Common mistakes
The mistake
Writing vague commit messages like 'fix' or 'changes'
The fix
A commit message should explain WHY the change was made, not just what changed (the diff shows that). Use Conventional Commits (feat:, fix:, docs:) for consistency and write a clear, specific message.
The mistake
Amending a commit that has already been pushed to a shared remote
The fix
git commit --amend rewrites history. If you have already pushed the commit, amending will cause conflicts for anyone who pulled it. Only amend local commits that have not been pushed.
Related Resources
Related Snippets
- Git Log Snippet
Viewing commit history after you have made commits.
Related Cheatsheets
- Git Cheatsheet
git commit options and message conventions.
Practice git commit
2 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.