I’ve been using Git for over eight years, and I probably use the same 15 commands for 95% of my work. Here they are, with actual scenarios where you’d use them.
The Daily Basics
1. Check what’s changed
git status # What files changed?
git diff # What exactly changed in those files?
git diff --staged # What's about to be committed?
2. Save your work
git add -p # Add changes interactively (my favorite)
git commit -m "Fix login timeout bug" # Commit with a message
Pro tip: git add -p lets you review each change before staging it. Way better than git add . which stages everything blindly.
3. Stay updated
git pull --rebase origin main # Get latest changes, put yours on top
git fetch --all # Download updates without merging
Branching (Where Things Get Interesting)
4. Create and switch branches
git checkout -b feature/user-auth # Create new branch and switch to it
git switch main # Switch back to main
git branch -d feature/user-auth # Delete merged branch
5. Merge and rebase
# When your feature branch is ready:
git checkout main
git merge feature/user-auth
# Or rebase for cleaner history:
git checkout feature/user-auth
git rebase main
Fixing Mistakes (We All Make Them)
6. Undo the last commit but keep changes
git reset --soft HEAD~1 # Uncommit but keep changes staged
7. Discard changes in a file
git checkout -- path/to/file.js # Revert file to last commit
8. Stash work temporarily
git stash # Save current work for later
git stash pop # Bring it back
git stash list # See all stashed work
I use stash constantly when someone asks me to review their PR and I’m in the middle of something.
Investigation Commands
9. Find who changed a line
git blame src/auth/login.js # Who wrote each line?
git log --oneline -20 # Last 20 commits, compact view
10. Search through commit history
git log --grep="fix login" # Find commits mentioning "fix login"
git log -S "getUserById" # Find when a string was added/removed
The Ones That Save You in Emergencies
11. Cherry-pick a specific commit
git cherry-pick abc1234 # Apply one specific commit to current branch
Useful when a bugfix on one branch needs to go to production immediately.
12. See what happened (when everything broke)
git reflog # Shows EVERYTHING you did, even "lost" commits
Reflog has saved me more times than I care to admit. Even if you accidentally delete a branch, the commits are still there in the reflog for 30 days.
One Last Tip
Set up good aliases in your ~/.gitconfig:
[alias]
s = status
co = checkout
br = branch
lg = log --oneline --graph --decorate -20
You’ll save thousands of keystrokes over a career. Git is one of those tools where a small investment in learning pays off every single day.
