Git commit Command
In this page:
Committing with a Message
git commit -m "message" saves everything currently staged as a new permanent snapshot in your project's history. The message isn't optional busywork -- a specific, honest description of what changed is what makes git log useful months later.
Example: Committing with a Message
git commit -m "Fix login button alignment"
Staging and Committing in One Step
git commit -am "message" combines staging and committing for files Git already tracks, skipping the separate git add step -- but note the shortcut only picks up modifications to tracked files, it will never stage a brand-new untracked file for you.
Example: Staging and Committing in One Step
git commit -am "Update styles"
Amending the Last Commit
git commit --amend replaces your most recent commit entirely -- with a corrected message, an added forgotten file, or both -- rather than creating a new commit. It's convenient for fixing a mistake seconds after making it, but avoid amending a commit you've already pushed and shared.
Example: Amending the Last Commit
git commit --amend
Committing with a Detailed Description
For a change that needs more explanation than one line, running git commit -m "Title" -m "Longer paragraph explaining why" creates a commit with a short summary line and a separate detailed body, matching the conventional format most Git tools expect.
Example: Committing with a Detailed Description
git commit -m "Fix login bug" -m "The button was not responding on mobile Safari due to a missing touch event handler."
Empty Commits
git commit --allow-empty creates a commit with zero file changes, which sounds useless but is genuinely handy for triggering a CI/CD pipeline manually, or for marking a meaningful milestone in history without altering any code.
Example: Empty Commits
git commit --allow-empty -m "Trigger CI pipeline"
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)