5 Git Tricks That Make You Look Like a Senior Dev
Most developers use about 20% of Git's power. These 5 commands are the other 80%. Master them and you'll solve problems in seconds that others spend hours on.
Interactive Rebase - Clean Commit History
Never push a messy branch again. Interactive rebase lets you squash, reorder, and edit commits before anyone sees them.
git rebase -i HEAD~4
This opens an editor with your last 4 commits. Change pick to squash to combine commits, or reword to fix a message. Your PR reviewer will thank you.
Git Bisect - Find the Bug in O(log n)
A bug was introduced sometime in the last 100 commits. Instead of checking them one by one, let Git binary-search for you:
git bisect start
git bisect bad HEAD # current state is broken
git bisect good v1.0.0 # last known good state
Git checks out a commit in the middle. Test it, mark it git bisect good or git bisect bad. In 7 steps, you'll find the exact commit among 100. Then git bisect reset to return.
Worktrees - Work on Two Branches Simultaneously
Need to switch branches but don't want to stash your changes? Worktrees let you check out multiple branches into separate directories - all from one repo.
git worktree add ../feature-branch feature-branch
Now you have two working directories: your current one and ../feature-branch. Make changes in either, they share the same .git folder. Perfect for hotfixes while deep in a feature.
Reflog - Your Undo Safety Net
Accidentally deleted a branch? Force-pushed the wrong thing? git reflog shows every action Git has taken in the last 90 days:
git reflog
# abc1234 HEAD@{0}: commit: fix login bug
# def5678 HEAD@{1}: reset: moving to HEAD~3
# ghi9012 HEAD@{2}: commit: add payment feature
Find the commit you lost and do git checkout ghi9012 to recover it. The reflog has saved more careers than any StackOverflow answer.
Partial Staging - Commit Specific Lines
You made 5 changes in one file but only want to commit 2 of them. Don't copy-paste into a new file. Use interactive staging:
git add -p
Git shows each chunk and asks what to do. Press y to stage it, n to skip, or s to split a large chunk into smaller ones. Commit exactly what you mean to commit.
Bottom Line
Start with git add -p and git reflog - you'll use those weekly. Then add rebase -i for your next PR. Bisect and worktrees are your secret weapons for the tough debugging sessions. Senior developers don't memorize more commands. They know fewer commands that solve problems faster.
Comments
No comments yet. Start the discussion.