Git Commands I Wish I Knew Earlier
Beyond add, commit, push. The commands that actually save time.
Undo Everything
# Unstage a file (keep changes)
git reset HEAD file.js
# Discard all unstaged changes in a file
git checkout -- file.js
# or (newer):
git restore file.js
# Uncommit (keep changes staged)
git reset --soft HEAD~1
# Uncommit AND unstage (keep changes)
git reset HEAD~1
# Discard commit entirely (dangerous!)
git reset --hard HEAD~1
Stashing — Save Work for Later
# Save current work temporarily
git stash
git stash push -m "WIP: feature X"
# List all stashes
git stash list
# Restore the most recent stash
git stash pop
# Apply without removing from stack
git stash apply
# Drop specific stash
git stash drop stash@{2}
# Clear all stashes
git stash clear
# Practical use case:
# You're working on feature A, need to fix a bug urgently
git stash # Save feature A work
git checkout -b hotfix/bug-123
# ... fix bug, commit, push ...
git checkout feature-A
git stash pop # Resume where you left off
Interactive Rebase — Clean Your History
# Edit last 3 commits before pushing
git rebase -i HEAD~3
# Options that appear:
pick abc1234 First commit
squash def5678 Second commit # Merge into previous
reword ghi9010 Third commit # Change message only
drop jkl1122 Fourth commit # Remove entirely
edit mno3456 Fifth commit # Pause to edit files
# Real example: Squash "typo fix" commits into main commit
git rebase -i origin/main
# Change 'pick' to 'squash' on the typo-fix commits
# Save → write new combined message → done!
Cherry-Pick — Grab Specific Commits
# Copy one commit from another branch
git cherry-pick abc1234
# Copy multiple commits
git cherry-pick abc1234 def5678 ghi9010
# Use case: Hotfix from dev branch to production
git checkout production
git cherry-pick abc1234 # Just this one fix
git push origin production
Bisect — Find the Breaking Commit
# Start bisect when you find a bug
git bisect start
git bisect bad # Current version has bug
git bisect good v1.0.0 # This version was fine
# Git will checkout a middle commit
# Test it, then tell git:
git good # This commit is fine
git bad # This commit has bug
# Repeat until Git finds the exact commit
# Result: abc1234 is the first bad commit
git bisect reset # Done
Search Through History
# Find commits by message
git log --grep="fix login"
# Find commits that touched a specific file
git log --oneline -- src/auth/login.js
# Find who changed a line (blame!)
git blame src/utils.js
# Search code content across all history
git grep -n "TODO" $(git rev-list --all)
# Find commits that added or removed a string
git log -S "functionName" --oneline
# Show diff of what changed between two points
git diff abc1234..def5678 --stat
Branch Management
# Rename current branch
git branch -m old-name new-name
# Delete local branch
git branch -d feature-x # Safe (checks if merged)
git branch -D feature-x # Force delete
# Delete remote branch
git push origin --delete feature-x
# Track a new remote branch
git checkout --track origin/new-feature
# See all branches sorted by last commit date
git branch -v --sort=-committerdate
# Clean up stale branches (merged into main)
git branch --merged main | grep -v '^\*\|main' | xargs git branch -d
Working With Remotes
# See all remotes
git remote -v
# Add a new remote
git remote add upstream https://github.com/original/repo.git
# Fetch from upstream (for forks)
git fetch upstream
git merge upstream/main
# Force push (use carefully!)
git push --force-with-lease # Safer than --force
# Only forces if your local ref matches expected remote state
# Push to a different remote branch
git push origin feature-x:hotfix/x
# Pull but rebase instead of merge (cleaner history)
git pull --rebase origin main
Reflog — Recover Lost Work
# See all actions (including "deleted" stuff)
git reflog
# Recover a deleted commit
git reset --hard HEAD@{5} # Go back to action #5
# Recover after force push disaster
git reflog # Find the commit before force-push
git reset --hard abc1234 # Reset to that commit
git push --force # Push it back
Aliases — Type Less
# Add these to ~/.gitconfig
[alias]
co = checkout
br = branch
ci = commit
st = status
lg = log --oneline --graph --all
unstage = reset HEAD --
amend = commit --amend --no-edit
last = log -1 HEAD
visual = log --oneline --graph --all --decorate
undo = reset HEAD~1
save = stash push -m
pop = stash pop
# Now you can type:
git co feature-x # instead of git checkout feature-x
git ci -m "Fix" # instead of git commit -m "Fix"
git lg # Beautiful graph view
Quick Reference Card
| Task | Command |
|---|---|
| Unstage | git reset HEAD <file> |
| Discard changes | git restore <file> |
| Undo last commit | git reset --soft HEAD~1 |
| Stash work | git stash |
| Pop stash | git stash pop |
| Squash commits | git rebase -i |
| Pick commit | git cherry-pick <hash> |
| Find breaking commit | git bisect |
| Search messages | git log --grep="text" |
| Search code | git -S "text" |
| Blame line | git blame <file> |
| Recover lost |
git reflog + git reset
|
What's your favorite Git command? Share it below!
Follow @armorbreak for more developer content.
Top comments (0)