Git Commands I Wish I Knew Earlier
Source: Dev.to
Stashing Changes
When you’re deep into a feature and need a clean working directory for an urgent review:
# Save all your changes to the stash
git stash
# Do your other work...
# Bring your changes back
git stash pop
Useful stash variations
# Stash with a descriptive message
git stash save "halfway through login refactor"
# View all stashes
git stash list
# Apply a specific stash without removing it from the list
git stash apply stash@{2}
# Stash only specific files
git stash push -m "navbar changes" src/components/Navbar.jsx
# Stash including untracked files
git stash -u
Bisecting to Find Bugs
When tests that were passing a week ago suddenly fail, you can pinpoint the offending commit with binary search:
# Start the bisect session
git bisect start
# Mark the current (broken) commit as bad
git bisect bad
# Mark a known good commit (e.g., from last week)
git bisect good a1b2c3d
# Git checks out the middle commit; test it, then tell Git:
git bisect good # if this commit works
git bisect bad # if this commit is broken
# Repeat until Git finds the exact bad commit
git bisect reset
If you have an automated test script:
git bisect start HEAD a1b2c3d
git bisect run npm test
Git will automatically locate the broken commit.
Interactive Rebase
When a branch contains a series of small or typo‑fix commits that you’d like to clean up before merging:
# Rebase the last 5 commits interactively
git rebase -i HEAD~5
The editor opens with a list like:
pick a1b2c3d Add user authentication
pick b2c3d4e Fix typo in auth
pick c3d4e5f Fix typo again
pick d4e5f6g Add logout feature
pick e5f6g7h Forgot semicolon
You can change the commands:
pick a1b2c3d Add user authentication
squash b2c3d4e Fix typo in auth
squash c3d4e5f Fix typo again
pick d4e5f6g Add logout feature
fixup e5f6g7h Forgot semicolon
Command meanings
squash (s): Merge commit with the previous one, combine messages.fixup (f): Merge commit with the previous one, discard this message.reword (r): Keep commit, but edit the message.drop (d): Delete the commit entirely.edit (e): Pause to amend the commit.
⚠️ Warning: Never rebase commits that have already been pushed to a shared branch.
Handy Git Aliases
Add these to ~/.gitconfig to speed up everyday tasks:
[alias]
# Shorter status
s = status -sb
# Pretty log graph
lg = log --oneline --graph --decorate --all
# Undo the last commit but keep changes
undo = reset --soft HEAD~1
# Amend without editing message
oops = commit --amend --no-edit
# Show what you did today
today = log --since='midnight' --author='Your Name' --oneline
# Delete all merged branches (except main/master)
cleanup = "!git branch --merged | grep -v '\\*\\|main\\|master' | xargs -n 1 git branch -d"
# Quick commit with message
cm = commit -m
# Create and switch to a new branch
cob = checkout -b
Now git s replaces git status --short --branch.
Amending Commits
Forgot to add a file or need to fix a commit message?
# Add forgotten file to the last commit
git add forgotten-file.js
git commit --amend --no-edit
# Or just fix the message
git commit --amend -m "Better commit message"
Recovering Lost Work
If you accidentally delete a branch or reset too hard, the reflog remembers everything:
git reflog
Typical output:
a1b2c3d HEAD@{0}: reset: moving to HEAD~3
b2c3d4e HEAD@{1}: commit: Add feature
...
Recover a previous state:
git checkout b2c3d4e
# or
git reset --hard b2c3d4e
Cherry‑picking a Specific Commit
git cherry-pick a1b2c3d
Finding Commits
# Search commit messages
git log --grep="bug fix"
# Search code changes for a specific string
git log -S "functionName"
# Show commits that touched a specific file (including renames)
git log --follow -p -- path/to/file.js
Cleaning Untracked Files
Dry run first:
git clean -n
Delete untracked files:
git clean -f
Delete untracked files and directories:
git clean -fd
Quick Reference Table
| Command | What It Does |
|---|---|
git stash | Save changes temporarily |
git stash pop | Restore stashed changes |
git bisect | Binary search for bugs |
git rebase -i HEAD~n | Interactive rebase last n commits |
git commit --amend | Modify the last commit |
git reflog | View history of all HEAD changes |
git cherry-pick | Copy a specific commit |
git log -S "text" | Find commits changing specific text |
git clean -fd | Remove untracked files and directories |
Git has a steep learning curve, but mastering these commands transformed my workflow. I went from fearing Git to actually enjoying version control (most of the time). Pick one command from this list and start using it this week; once it becomes muscle memory, add another. Happy coding! 🚀