Git Commit Explained – A Beginner’s Guide to Saving Changes in Git
What is git commit?
git commit is like taking a snapshot 📷 of your code at a particular moment in time. It permanently records your staged changes into Git history — like saying, “This is what my project looked like right now.”
You first stage your changes using git add, then commit them using:
git commit -m "Your message here"
The Git Workflow in Action
Here’s a visual overview of the basic Git workflow:
Working Directory
git add
Staging Area
git commit
Git Repository (e.g., GitHub)
You work in the Working Directory, prepare changes in the Staging Area, then save them using git commit.
Example Git Flow
git add file.html # Stage changes
git commit -m "Added login form" # Save snapshot
git add: “Let’s include this in the next photo.”
git commit: “Click! Done. Snapshot saved.”
Preparing to Commit
Before you commit, you need to stage files using:
git add index.html
To check what’s staged or not:
git status
It’ll show you:
- Untracked files
- Modified files
- Staged files
Basic git commit Syntax
Use -m to Add a Message
git commit -m "Describe your change"
Good:git commit -m "Update header layout for mobile responsiveness"
Bad:git commit -m "stuff fixed"
Commit messages are your future memory — be specific!
Real-World Example
Scenario
You edited your homepage and styles:
git add index.html style.css
git commit -m "Redesigned homepage and improved mobile responsiveness"
Now the change is saved in your Git history forever
Why Good Commit Messages Matter
- Debug faster: Helps trace issues to specific changes.
- Understand history: Commit messages are documentation.
- Collaborate better: Teammates can easily follow your work.
Useful git commit Options
Auto-stage and Commit in One Go
git commit -am "Quick fix to navbar padding"
Works only for files Git already tracks.
Fix the Last Commit
git commit --amend
Useful for correcting typos or adding missed files.
Multi-line Commit Messages
Just run:
git commit
Git opens your editor for detailed commit messages. Use this when one-liners aren’t enough.
Understanding Commit History
View Full Log
git log
Shows all commits, authors, and dates.
View Summary Format
git log --oneline
Gives a condensed view of your project’s commit history — perfect for quick scanning.
Undoing Commits
⏮️ Soft Reset (Undo Last Commit, Keep Changes)
git reset --soft HEAD~1
The files stay modified, but the commit is undone.
Revert a Commit (Safe Undo)
git revert <commit-id>
This creates a new commit that reverses the effect of the previous one — safe and team-friendly!
Common Mistakes to Avoid
- Forgetting to
git addbefore committing
Always check withgit status - Committing sensitive files like
.env
Use.gitignoreto exclude them - Writing vague commit messages
Be descriptive and intentional
Tips for Team Collaboration
Use Conventional Commit Messages
feat: add user login functionality
fix: correct alignment bug in header
Link Commits to Issues
git commit -m "Fixes #42 - Updated FAQ layout"
Helps you connect commits to your issue tracker (like GitHub Issues or Jira).
Git GUI vs CLI
🖱 GUI Tools
- GitHub Desktop – Beginner-friendly and clean UI
- Sourcetree – Powerful visual branching/staging
- GitKraken – Visually stunning Git workflow manager
CLI is King
The command line gives you full control, scriptability, and better understanding of Git internals
Summary: Git Commit
-
git commitcaptures your code at a point in time - Use
-mfor meaningful commit messages - Use
--amend,-a, and--onelineto streamline workflow - Track your progress with
git log
Committing isn’t just about saving — it’s about telling the story of your code.
Frequently Asked Questions: Git Commit
1. What’s the difference between git add and git commit?
git add stages the file for commit
git commit permanently saves it to Git history
2. Can I edit an old commit?
Yes.
Use git commit --amend to fix the last one.
Use git rebase to rewrite older ones (advanced).
3. What happens if I forget to commit?
Your changes remain staged or unstaged — they’re not lost. Just run:
git add file.js
git commit -m "Your message"
4. Can I commit multiple files at once?
Absolutely!
git add file1.html file2.css
git commit -m "Updated UI components"
5. How do I delete a commit?
Use:
git reset HEAD~1 # Dangerous, rewrites history
git revert <id> # Safe and recommended for teams
Share Now :
