Git Commit
Last updated
git commit records the changes you've staged into the project's history as a new commit, with a message describing what changed. You stage changes first with git add, then commit them - or use -am to stage tracked files and commit in one step. Each commit is a snapshot you can return to later.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git commit -m "message" | Commit staged changes with a message |
git commit -am "message" | Stage tracked files and commit in one step |
git commit | Commit and open your editor for the message |
git commit --amend | Edit the most recent commit |
git commit -m "title" -m "body" | Commit with a title and a longer body |
Common cases
| Goal | Command |
|---|---|
| Commit everything tracked | git commit -am "message" |
| Commit only staged files | git commit -m "message" |
| Fix the last commit's message | git commit --amend |
| Commit an empty commit | git commit --allow-empty -m "msg" |
Git commit FAQ
How do I commit changes in Git?
Stage the changes you want with
git add <file> (or git add . for everything), then run git commit -m "your message". The commit records a snapshot of the staged changes into history. To stage and commit tracked files in one step, use git commit -am "your message".What's the difference between git commit -m and -am?
-m lets you pass the commit message inline so Git doesn't open an editor. -am combines -a (stage all modified and deleted tracked files) with -m, so it stages and commits in one command. Note -a does not include brand-new untracked files - those still need an explicit git add.How do I write a good commit message?
Write a short imperative summary ("Add login validation") under about 50 characters, then an optional blank line and a body explaining why. Pass a body with a second
-m: git commit -m "Add login validation" -m "Prevents empty submissions", or omit -m entirely to write it in your editor.How do I change my last commit?
Use
git commit --amend. It reopens the most recent commit so you can edit its message or add files you forgot to stage. Avoid amending a commit you've already pushed, since it rewrites history - see the git commit --amend page for details.Can I practice this online?
Yes. Open the terminal playground to run
git commit in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers staging and committing step by step.