git commit Command Guide
Record changes to the repository
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git commit -m "msg"Commit with messagegit commitOpen editor for messagegit commit -aStage and commit trackedModify
--amendModify last commit--amend --no-editAmend without changing msg--fixup <commit>Create fixup commitOptions
-vShow diff in editor--allow-emptyAllow empty commit-SGPG sign commitDownloadable Image Preview
Basic Usage
The git commit command records changes to the repository. It creates a snapshot of the staged changes along with a descriptive message.
# Commit with inline message
git commit -m "Add user authentication feature"
# Open editor for commit message
git commit
# Stage tracked files and commit in one step
git commit -am "Fix login bug"Writing Good Commit Messages
Good commit messages help you and your team understand the project history:
# Short, descriptive message
git commit -m "Add password reset functionality"
# Multi-line message (via editor or quotes)
git commit -m "Add password reset functionality
- Add reset email template
- Create reset token generation
- Add expiration validation"Commit Message Best Practices
- • Keep the first line under 50 characters
- • Use imperative mood: "Add feature" not "Added feature"
- • Explain what and why, not how
- • Reference issues: "Fix login bug (#123)"
Common Options
| Option | Description |
|---|---|
| -m <message> | Use the given message as commit message |
| -a, --all | Stage modified and deleted files automatically |
| --amend | Modify the most recent commit |
| -v, --verbose | Show diff in the commit message editor |
| --no-verify | Skip pre-commit and commit-msg hooks |
| --allow-empty | Allow creating a commit with no changes |
Amending Commits
Use --amend to modify the most recent commit:
# Change the last commit message
git commit --amend -m "New commit message"
# Add forgotten files to the last commit
git add forgotten-file.js
git commit --amend --no-edit
# Amend and edit the message in editor
git commit --amendPractical Examples
# Standard workflow
git add src/feature.js
git commit -m "Add new feature"
# Quick commit for all tracked changes
git commit -am "Fix typo in README"
# Commit with verbose diff display
git commit -v
# Create an empty commit (useful for CI triggers)
git commit --allow-empty -m "Trigger rebuild"
# Sign commit with GPG
git commit -S -m "Signed commit"
# Commit with co-author
git commit -m "Add feature
Co-authored-by: Name <email@example.com>"Frequently Asked Questions
How do I change the last commit message?
Use "git commit --amend" to open an editor to modify the last commit message. If you've already pushed, you'll need to force push (be careful with shared branches).
How do I add forgotten files to the last commit?
Stage the files with "git add <file>" then run "git commit --amend --no-edit" to add them to the last commit without changing the message.
What is a good commit message format?
Use a short summary (50 chars or less), optionally followed by a blank line and a more detailed description. Start with an imperative verb like "Add", "Fix", "Update".
How do I commit only some changes from a file?
Use "git add -p <file>" to interactively stage specific hunks, then commit normally. This allows you to split changes into separate commits.
Can I undo a commit?
Yes! Use "git reset --soft HEAD~1" to undo the last commit while keeping changes staged, or "git reset HEAD~1" to unstage them. Use "git revert" for a safe undo that creates a new commit.
Summary
git commit creates permanent snapshots of your changes. Write clear, descriptive messages to maintain a useful project history.
Quick Reference
git commit -m "msg"- Commit with messagegit commit -am "msg"- Stage and commitgit commit --amend- Modify last commitgit commit -v- Commit with diff view
Official Documentation
For authoritative information, refer to the official documentation: