Git — Commit
What is a Commit?
A commit is a snapshot of your project at a specific point in time. It records what changed, who made the change, when, and why.
The Two-Step Process
# Step 1: Stage changes
git add filename.txt
# Step 2: Commit
git commit -m "Add login page"
Why Stage First?
Staging lets you choose exactly which changes to include:
# You modified 3 files, but only want to commit 2
git add file1.txt file2.txt
git commit -m "Update file1 and file2"
Staging Files
# Stage a specific file
git add index.html
# Stage multiple files
git add index.html style.css script.js
# Stage all changes in current directory
git add .
# Stage all changes everywhere
git add -A
# Interactive staging (choose parts of files)
git add -p
Writing Commit Messages
Good Messages
git commit -m "Add user authentication"
git commit -m "Fix login button not responding on mobile"
git commit -m "Update README with installation steps"
git commit -m "Remove deprecated API endpoint"
Bad Messages
git commit -m "fixed stuff"
git commit -m "update"
git commit -m "asdfgh"
git commit -m "WIP"
Convention
<type>: <short description>
<optional body>
Types:
feat— new featurefix— bug fixdocs— documentationstyle— formattingrefactor— code restructuringtest— adding testschore— maintenance
git commit -m "feat: add password reset functionality"
git commit -m "fix: resolve mobile menu not closing"
git commit -m "docs: update API documentation"
Viewing Commits
# Show recent commits
git log --oneline
# Show with details
git log --stat
# Show changes in each commit
git log -p
# Show last 5 commits
git log -5
# Show commit history as graph
git log --graph --oneline --all
Amending Commits
# Change the last commit message
git commit --amend -m "New message"
# Add forgotten files to last commit
git add forgotten-file.txt
git commit --amend --no-edit
Undoing Commits
# Undo last commit, keep changes staged
git reset --soft HEAD~1
# Undo last commit, keep changes unstaged
git reset HEAD~1
# Undo last commit, discard changes (DANGEROUS)
git reset --hard HEAD~1
Commit Hashes
Each commit gets a unique 40-character hash:
# View full hashes
git log --format="%H"
# Use short hash
git show abc1234
# Reference commits
git diff abc1234 def5678
Practical Example
# Start a new feature
git checkout -b feature/login
# Make changes
echo "login form" > login.html
echo "styles" > login.css
# Stage and commit
git add login.html login.css
git commit -m "feat: add login page with form and styles"
# Make more changes
echo "validation" > login.js
git add login.js
git commit -m "feat: add form validation"
# Switch back to main
git checkout main
# Merge the feature
git merge feature/login
Best Practices
- Commit often — small, focused commits are easier to understand
- Don't commit half-done work — use
git stashinstead - Write meaningful messages — future you will thank present you
- One logical change per commit — don't mix unrelated changes
- Never commit generated files — add them to
.gitignore
Mini Practice
- Create a file, stage it, and commit with a message
- Modify the file, check
git status, then commit again - Use
git logto view your commit history - Amend a commit message with
--amend - Create a branch, make commits, and merge back
Up Next
Next: Branches →
Related Topics
Frequently Asked Questions about Commit
What is Commit in Git?
Commit is a fundamental concept in Git. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Commit?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Commit.
Why is Commit important in Git?
Commit is essential for Git development. Understanding this concept will help you write better code and solve real-world problems more effectively.