TL;DR — Quick Summary
Master advanced Git techniques to maintain a clean history, automate tasks, and quickly debug regressions using rebase, bisect, and hooks.
Moving Beyond Basic Git
While git commit, git push, and git pull are enough for individual projects, collaborating on large professional codebases requires a deeper understanding of version control. Mastering advanced Git tools reduces merge conflicts, automates code quality, and drastically cuts debugging time.
1. Git Rebase: Maintaining a Linear History
Rebasing is the process of moving or combining a sequence of commits to a new base commit.
Imagine you are working on a feature branch while a colleague merges updates into main. Instead of using git merge main (which creates a messy merge commit), you can rebase your feature on top of the latest main.
How to Rebase
# Switch to your feature branch
git checkout feature
# Rebase it onto main
git rebase main
Git will lift your new commits, update your branch to match main, and re-apply your commits on top. Golden Rule: Never rebase a shared, public branch.
Interactive Rebase (Squashing Commits)
If you have 10 messy “WIP” commits, you can squash them into one clean commit before creating a Pull Request.
# Interactively rebase the last 5 commits
git rebase -i HEAD~5
Your editor will open:
pick 1234567 Added login button
squash 2345678 Fixed login CSS
squash 3456789 Whoops, typo in login
Changing pick to squash merges those commits into the one above it.
2. Git Bisect: The Debugging Superpower
You pull the latest main branch and realize a feature is broken. Looking at the git log, there are 150 new commits since yesterday. Who broke it?
Git Bisect performs a binary search through your commit history to find the exact commit that introduced the bug.
Bisect Workflow
- Start the process:
git bisect start - Mark the current state as broken:
git bisect bad - Find an older commit that works, and mark it:
git checkuot HEAD~50 # go back 50 commits, test it. It works! git checkout main git bisect good <working-commit-hash> - Test and repeat: Git will check out a commit exactly in the middle. You test the app.
- If it works:
git bisect good - If it’s broken:
git bisect bad
- If it works:
- Git will automatically halve the search space again until it tells you exactly which commit caused the issue.
- Clean up:
git bisect reset
3. Git Hooks: Automating Quality
Git hooks are custom scripts that Git executes before or after events such as commit, push, and receive.
They are stored in the .git/hooks directory of your repository.
Example: A pre-commit Hook
A pre-commit hook runs before you even type a commit message. It’s perfect for running linters or tests to prevent bad code from entering the repository.
- Create a file at
.git/hooks/pre-commit - Make it executable:
chmod +x .git/hooks/pre-commit - Add your script:
#!/bin/sh
# pre-commit hook
echo "Running tests..."
npm test
# If tests fail ($? is not 0), abort the commit
if [ $? -ne 0 ]; then
echo "Tests must pass before commit!"
exit 1
fi
Now, if npm test fails, the commit is completely blocked.
Summary
- Use Rebase (
git rebase -i) to keep your branch history clean and linear before sharing code. - Use Bisect (
git bisect) to mathematically narrow down the exact commit that introduced a regression. - Use Hooks (
.git/hooks/pre-commit) to mathematically enforce code quality and automate tasks natively.