Chapter 42: Git Advanced
Git Advanced is everything that comes after you feel comfortable with those commands and you start asking questions like:
- “How do I clean up this messy branch before showing it to the team?”
- “I committed secrets three weeks ago — how do I erase them forever?”
- “Can I reorder / combine / split commits retroactively?”
- “How do I move only part of my work to another branch?”
- “What is the real difference between rebase and merge in large teams?”
- “How do I recover after a horribly failed rebase?”
Today I’ll walk you through the most powerful & most used advanced techniques — explained slowly, with real examples, warnings, mental models, and the exact commands people use in serious projects in 2026.
1. Interactive Rebase – The King of History Cleanup
git rebase -i = “interactive rebase” → your personal commit surgery table.
You can:
- reorder commits
- squash several into one
- fixup (squash without changing message)
- edit commit message
- drop (delete) commits
- split one commit into many
- reword messages
Realistic example — messy feature branch before PR
You worked fast and now have this ugly history:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
git log --oneline # abc1234 fix typo # def5678 oops forgot import # 456def7 add dark mode toggle # 789abcd wip dark mode # fedcba9 start dark mode |
You want to present clean history to reviewers:
- Squash the three “dark mode” commits into one
- Combine fix + forgot import
- Better messages
Run:
|
0 1 2 3 4 5 6 7 |
git rebase -i origin/main # or git rebase -i HEAD~5 (last 5 commits) |
Git opens your editor with this list (oldest at top):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
pick fedcba9 start dark mode pick 789abcd wip dark mode pick 456def7 add dark mode toggle pick def5678 oops forgot import pick abc1234 fix typo # Rebase abc1234..fedcba9 onto origin/main (5 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. |
Change it to:
|
0 1 2 3 4 5 6 7 8 9 10 |
pick fedcba9 start dark mode squash 789abcd wip dark mode squash 456def7 add dark mode toggle pick def5678 oops forgot import fixup abc1234 fix typo |
Save & close → Git starts replaying:
- Keeps “start dark mode”
- Squashes next two into it → asks for new message
- Keeps “oops forgot import”
- Fixup (squash silently) the typo commit into it
You get to edit the combined dark-mode message:
|
0 1 2 3 4 5 6 7 8 9 10 |
feat: implement dark mode toggle with localStorage persistence - Added CSS variables & toggle button - Persist choice in localStorage - Apply on page load |
Then new history:
|
0 1 2 3 4 5 6 7 8 9 |
git log --oneline # new-hash feat: implement dark mode toggle ... # new-hash fix: add missing import + typo fix # older commits ... |
→ Clean, professional history — reviewers happy.
Warning: Never interactive-rebase commits that others already pulled/pushed.
2. git cherry-pick – Transplant single commits
You need one specific commit from another branch — not the whole branch.
Example:
Branch experiment/refactor-login has one perfect commit:
|
0 1 2 3 4 5 6 7 8 |
git log experiment/refactor-login --oneline # 123abcd refactor login validation logic # ... |
You want only that commit on main:
|
0 1 2 3 4 5 6 7 |
git switch main git cherry-pick 123abcd |
→ New commit with same changes, new hash, on main.
Very useful when:
- Hotfix from release branch to main
- One good commit from messy branch
- Backporting bugfix to older release branch
3. git filter-branch / git filter-repo – Remove secrets from history
You committed a .env file with API key three months ago.
Modern way (2026 standard) = git filter-repo (faster & safer than old filter-branch)
Install (one time):
|
0 1 2 3 4 5 6 |
pip install git-filter-repo |
Remove file from entire history:
|
0 1 2 3 4 5 6 |
git filter-repo --invert-paths --path .env |
→ Every commit that touched .env is rewritten → file gone forever → New commit hashes for whole repo
Then force-push (dangerous!):
|
0 1 2 3 4 5 6 7 |
git push --force --all git push --force --tags |
Warning: Everyone who pulled before must re-clone or reset — breaks shared history.
Safer alternatives:
- git revert the commit that added the secret
- Rotate the leaked key immediately
- Use BFG Repo-Cleaner (easier for binaries/secrets)
4. git worktree – Work on two branches at once (no stash needed)
You are on main but want to quickly fix something on release/v1.2 without stashing or switching.
|
0 1 2 3 4 5 6 |
git worktree add ../hotfix-release-v1.2 release/v1.2 |
Now you have two working directories:
- Current folder → main
- ../hotfix-release-v1.2 → release/v1.2
Work in both simultaneously — no switching, no stash.
Remove when done:
|
0 1 2 3 4 5 6 |
git worktree remove ../hotfix-release-v1.2 |
Very powerful for hotfixes / reviewing old branches.
5. git bisect – Find which commit introduced a bug
Binary search through history to find the bad commit.
|
0 1 2 3 4 5 6 7 8 |
git bisect start git bisect bad # current HEAD is broken git bisect good 123abcd # this old commit was good |
Git checks out middle commit → test
|
0 1 2 3 4 5 6 7 8 9 10 |
# test fails → bad git bisect bad # test passes → good git bisect good |
Repeat 5–10 times → Git tells you exactly which commit introduced the bug.
Very powerful for debugging regressions.
6. Quick Advanced Git Cheat Sheet (2026 power tools)
| Task | Command / Pattern | When to use it |
|---|---|---|
| Clean messy personal branch before PR | git rebase -i origin/main | Squash / reorder / reword before push |
| Transplant one commit to another branch | git cherry-pick <hash> | Need only one good commit from messy branch |
| Remove file from all history | git filter-repo –invert-paths –path secrets.env | Leaked keys / large files — then force push |
| Work on two branches simultaneously | git worktree add ../hotfix release/v1 | Hotfix + feature at same time |
| Binary search for bug commit | git bisect start; bisect bad; bisect good … | Find when regression started |
| Undo almost anything locally | git reflog → git reset –hard <hash> | After reset / rebase / amend panic |
Got the advanced feeling now?
Git advanced = history rewriting, cleanup, recovery, and precision tools — used mostly on your own private branches
Next?
- Want to do a full interactive rebase demo (squash 6 commits into 2)?
- See git filter-repo remove secrets live?
- Or compare rebase vs merge vs cherry-pick head-to-head?
Just tell me — we’ll keep going. You’ve reached the level where Git becomes your superpower. Extremely proud of you! 🚀
