Git is more than just commit
and push
. While most developers use the basics daily, truly mastering Git unlocks a powerful toolkit for managing complex workflows, collaborating effectively, and recovering from mistakes. Whether you’re working solo or on a large-scale project, these advanced Git techniques can help you work faster, safer, and smarter.
1. Interactive Rebase for a Clean Commit History
Instead of leaving a messy trail of “fix typo” and “small change” commits, use interactive rebase to polish your commit history before merging.
git rebase -i HEAD~5
This opens an editor where you can:
- Reorder commits to group related changes.
- Squash commits into a single one.
- Edit commit messages for clarity.
A clean history makes reviewing and debugging far easier.
2. Stashing Changes (and Keeping Them Organized)
When you need to quickly switch branches without committing work in progress, git stash
is your friend:
git stash push -m "WIP: user profile redesign"
Later, you can list and reapply stashes:
git stash list
git stash apply stash@{0}
This keeps your work safe without polluting the commit history.
3. Bisecting to Find Bugs
If a bug appeared recently, binary search through commits to find the culprit using:
git bisect start
git bisect bad
git bisect good <commit-hash>
Git will automatically check out commits between the good and bad states, helping you pinpoint the exact change that introduced the problem.
4. Cherry-Picking Specific Commits
Need a single commit from another branch without merging the entire branch? Use cherry-pick
:
git cherry-pick <commit-hash>
This is especially useful when you want to apply hotfixes from one branch to another without pulling in unrelated changes.
5. Using Hooks for Automation
Git hooks let you run scripts automatically before or after certain actions—like committing or pushing. For example, you can add a pre-commit hook to run tests or lint code:
# .git/hooks/pre-commit
npm run lint
Make the script executable, and Git will enforce quality checks before accepting commits.
6. Partial Staging with Patch Mode
Sometimes you want to commit only part of a file. Use interactive patch mode:
git add -p
This lets you review changes hunk-by-hunk and choose exactly what goes into the commit.
7. Worktrees for Parallel Development
Instead of constantly stashing and switching branches, use worktrees to have multiple working directories linked to the same repository:
git worktree add ../feature-branch feature-branch
Now you can work on separate features in parallel without disrupting your main working copy.
8. Reflog for Time Travel
Even if you lose a branch or commit, Git’s reflog records every change to HEAD:
git reflog
You can recover lost work by checking out the commit hash from this log—perfect for “oops” moments.
9. Sparse Checkout for Large Repos
When working with massive monorepos, you can check out only specific folders:
git sparse-checkout init --cone
git sparse-checkout set path/to/subproject
This speeds up cloning and reduces local storage usage.
10. Advanced Merge Strategies
Git offers merge strategies like ours
and theirs
to handle complex merges where you want to favor one side:
git merge -X ours feature-branch
This tells Git to keep your branch’s changes in conflicts automatically.
Advanced Git skills can be a game-changer. They help keep your repository organized, your workflow efficient, and your team in sync. The more comfortable you are with these techniques, the less time you’ll spend wrestling with version control—and the more time you’ll have to focus on building great software.
Happy coding🚀