Git Mastery: Advanced Tips for Developers
Go beyond add, commit, and push. Learn advanced Git commands to manage your history and fix mistakes effectively.
Git Mastery: Advanced Tips for Developers
Most developers know the basics: git add, git commit, git push. But Git is a powerful beast with tools that can save you hours of headaches when things go wrong.
1. Interactive Rebase (git rebase -i)
Stop creating commits like “fix” and “fix again”. Use interactive rebase to squash multiple commits into one clean commit before merging.
git rebase -i HEAD~3
This opens your editor and lets you pick, squash, or edit the last 3 commits.
2. Git Bisect
Found a bug but don’t know when it was introduced? git bisect uses binary search to find the commit that broke your code.
git bisect start
git bisect bad # Current version is bad
git bisect good <commit-hash> # Last known good version
Git will check out a commit in the middle. You test it, tell git if it’s good or bad, and it repeats until it finds the culprit.
3. Cherry-pick
Need a specific fix from another branch but don’t want to merge the whole thing? cherry-pick applies a specific commit to your current branch.
git cherry-pick <commit-hash>
4. Reflog
Think you lost a commit? Git reflog tracks every move of HEAD. Even if you deleted a branch or reset a commit, it’s likely still in the reflog.
git reflog
git reset --hard HEAD@{index}
5. Stash with Name
Don’t just git stash. Name your stashes so you know what they are later.
git stash save "work in progress: sidebar navigation"
Conclusion
Mastering these commands transforms Git from a simple save button into a powerful time-machine for your code.