Master advanced Git workflows for professional development. Learn interactive rebase, cherry-pick, git bisect for debugging, stash management, and Git internals.
Git Advanced Workflows
Interactive Rebase
# Clean up last 5 commits before PR
git rebase -i HEAD~5
# In editor:
# pick abc1234 Add user model
# squash def5678 Fix typo in user model
# reword ghi9012 Add authentication
# fixup jkl3456 Fix auth bug
# drop mno7890 Debugging code
# Rebase onto main
git fetch origin
git rebase -i origin/main
# Rebase with autosquash (squash! and fixup! commits)
git commit --fixup HEAD~2 # Creates "fixup! Original commit message"
git rebase -i --autosquash origin/main
Cherry-Picking Strategies
# Cherry-pick a single commit
git cherry-pick abc1234
# Cherry-pick a range
git cherry-pick abc1234..def5678
# Cherry-pick without committing (for inspection)
git cherry-pick --no-commit abc1234
# Cherry-pick and resolve conflicts
git cherry-pick abc1234
# ... resolve conflicts ...
git add .
git cherry-pick --continue
# Cherry-pick preserving original author
git cherry-pick -x abc1234 # Adds "(cherry picked from commit...)" note
Git Bisect for Bug Hunting
# Find the commit that introduced a bug
git bisect start
git bisect bad HEAD # Current state is broken
git bisect good v2.0.0 # v2.0.0 was working
# Git checks out middle commit - test and mark
git bisect good # or 'bad'
# Automated bisect with test script
git bisect start HEAD v2.0.0
git bisect run npm test -- --testNamePattern "auth.test"
# Git will find the exact bad commit automatically
# Reset when done
git bisect reset
Stash Advanced Usage
# Stash with description
git stash push -m "WIP: user auth feature"
# Stash specific files
git stash push -p # Interactive patch mode
git stash push src/auth.ts src/user.ts
# List stashes
git stash list
# stash@{0}: WIP: user auth feature
# stash@{1}: On main: temp fix
# Apply specific stash without dropping
git stash apply stash@{1}
# Pop specific stash
git stash pop stash@{0}
# Create branch from stash
git stash branch feature/auth stash@{0}
# Show stash diff
git stash show -p stash@{0}
Worktrees for Parallel Work
# Work on multiple branches simultaneously
git worktree add ../hotfix-branch hotfix/critical-bug
git worktree add ../feature-branch feature/new-ui
# List worktrees
git worktree list
# Remove worktree when done
git worktree remove ../hotfix-branch
Git Hooks for Quality
#!/bin/bash
# .git/hooks/pre-commit (or use husky)
# Run linting
npm run lint
if [ $? -ne 0 ]; then
echo "Lint failed. Commit aborted."
exit 1
fi
# Run type check
npm run type-check
if [ $? -ne 0 ]; then
echo "Type check failed. Commit aborted."
exit 1
fi
# Run related tests
npm run test -- --findRelatedTests $(git diff --cached --name-only | tr '
' ' ')
// husky + lint-staged setup
// package.json
{
"lint-staged": {
"*.{ts,tsx}": ["eslint --fix", "prettier --write"],
"*.{js,jsx}": ["eslint --fix"],
"*.css": ["prettier --write"],
"*.json": ["prettier --write"]
}
}
Useful Git Aliases
# ~/.gitconfig
[alias]
lg = log --oneline --graph --decorate --all
co = checkout
br = branch
st = status -sb
undo = reset HEAD~1 --mixed
amend = commit --amend --no-edit
pushf = push --force-with-lease # Safe force push
recent = branch --sort=-committerdate
unstage = reset HEAD --
aliases = config --get-regexp alias
# One-line history with color
git log --oneline --graph --decorate --all --color=always | head -20
# Find commits touching a function
git log -L :functionName:path/to/file.ts
# Show commits by author
git log --author="John" --since="1 month ago" --oneline
Git Reflog for Recovery
# See all recent HEAD positions
git reflog
# Recover a dropped stash
git reflog stash
# Recover deleted branch
git checkout -b recovered-branch abc1234 # SHA from reflog
# Undo a bad rebase
git reflog # Find SHA before rebase
git reset --hard HEAD@{5} # Go back to pre-rebase state
Repository Maintenance
# Garbage collection
git gc --prune=now --aggressive
# Find large files
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sort -k3 -rn | head -20
# Remove large file from history (BFG)
bfg --strip-blobs-bigger-than 1M
git reflog expire --expire=now --all
git gc --prune=now --aggressive