Advanced Git Workflows Cheat Sheet

Quick reference guide for Advanced Git: Interactive rebase, cherry-pick, bisect, reflog recovery, and worktrees.

Version Control
git
git-rebase
reflog

Advanced Git features enable powerful commit history editing, emergency data recovery, automated bug hunting, and isolated parallel branch workspaces.

Interactive Rebase (`git rebase -i`)

Interactively edit, squash, reorder, or drop commits from history.

bash
# Interactively rebase last 5 commits
git rebase -i HEAD~5

Rebase Todo List Commands:

Table
KeywordShortAction
pickpUse commit as-is
rewordrUse commit, but edit commit message
editeUse commit, but stop for amending before moving on
squashsCombine commit into previous commit, meld messages
fixupfCombine commit into previous commit, discard message
dropdDelete commit entirely from history

Git Worktrees (`git worktree`)

Worktrees allow multiple active branches checked out in separate working directories simultaneously.

bash
# Add a new worktree directory for feature branch
git worktree add ../my-feature-dir -b feature/login

# List all active worktrees
git worktree list

# Remove worktree after work is completed
git worktree remove ../my-feature-dir

Reflog Emergency Data Recovery

git reflog records every HEAD movement (commits, rebases, checkouts), allowing recovery of deleted commits or branches.

bash
# View reflog history
git reflog

# Restore branch to commit before bad rebase
git reset --hard HEAD@{2}

Binary Search Bug Hunting (`git bisect`)

Automate finding the exact commit that introduced a bug using binary search.

bash
git bisect start
git bisect bad                 # Current commit is broken
git bisect good v1.0.0         # Known good commit or tag

# Test commit and mark
git bisect good # (or git bisect bad)

# Reset when finished
git bisect reset

Common Pitfalls & Tips

[!WARNING] Never run git rebase -i or git push --force on shared public branches like main or master!

[!TIP] Use git commit --fixup <commit-hash> followed by git rebase -i --autosquash to cleanly patch earlier commits automatically.