Git Notes
Complete guide to git commit command including commit messages, best practices, amending commits, commit history, and working with atomic commits for clean Git history.
git commit creates a permanent snapshot of staged changes in your repository. Writing good commit messages is crucial for maintainable projects.
Basic Commit
Interactive Commit (No Staging)
# Stage and commit in one command
git commit -a -m "Update database connection"
# Only works for modified files, not new filesMulti-line Commit Message
# Open editor for longer message
git commit
# In editor:
# Add user authentication system
# - Implements login/logout functionality
# - Uses JWT tokens for session management
# - Password hashing with bcrypt
# - Unit tests for auth moduleCommit Message Conventions
Poor Messages:
Good Messages:
| git commit -m "Fix | Resolve memory leak in user service" |
| git commit -m "Feat | Add email verification on signup" |
| git commit -m "Docs | Update API documentation" |
| git commit -m "Refactor | Simplify authentication logic" |
Commit Message Format
| - feat | New feature |
| - fix | Bug fix |
| - docs | Documentation |
| - style | Code formatting |
| - refactor | Code restructuring |
| - test | Adding tests |
| - chore | Maintenance tasks |
Amend Last Commit
# Forgot to include file or typo in message?
git add forgotten-file.js
git commit --amend
# Edit message in editor
# Replaces previous commit (local only!)View Commits
# Last 5 commits
git log -5
# One-liner view
git log --oneline
# With graph (branches)
git log --graph --oneline --all
# Pretty format
git log --pretty=format:"%Cred%h %Creset%s %Cgreen(%cr) %C(bold blue)<%an>%Creset"Atomic Commits
Good (atomic)
- Commit 1: Add login form HTML/CSS
- Commit 2: Implement login validation
- Commit 3: Add password hashing
- Commit 4: Add unit tests for login
Bad (bulk)
- Commit 1: "added login feature, updated db, fixed bugs, refactored auth"
Commit Lifecycle
Best Practices
- Commit frequently (multiple times per day)
- Write clear messages explaining WHY
- Keep commits focused on one feature/fix
- Review changes before committing
- Don't commit broken code
- Include related changes only
Interview Q&A
Q1: What should a good commit message contain?
A: Clear subject (50 chars max) explaining what changed. Body explaining WHY the change (wrapped at 72 chars). Type prefix (feat, fix, docs) for categorization. Avoid vague messages like "fixed stuff". Good message: "Fix: Resolve infinite loop in data parser" explaining problem and solution.
Q2: What are atomic commits?
A: Atomic commits are focused snapshots addressing single feature, fix, or task. For example, separate commits for HTML changes, logic changes, and test changes. Opposite of bulk commits combining unrelated changes. Better for code review, debugging, and reverting specific changes.
Q3: How do you amend a commit?
A: Use git commit --amend to modify last commit. Add forgotten files with git add, then --amend replaces previous commit. Use to fix typos in messages or include missed files. Important: only amend commits not yet pushed to shared repository.
Q4: Difference between git commit -a and git commit -m?
A: git commit -m commits staged changes with message provided directly. git commit -a stages all tracked modified files then commits in one step. git commit -a doesn't stage new files, only modifications. Most workflows use staged git add then git commit -m for control.
Q5: Why write good commit messages?
A: Good messages help future developers (including yourself) understand why code changed. Essential for debugging: git log shows decisions. For code review: explains intent. For maintenance: helps new developers understand codebase. Teams investing in commit message quality have better maintainability long-term.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Commit - Save Changes.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, basics, commit, git commit - save changes
Related Git & GitHub Topics