| name | git-advanced |
| description | Advanced Git operations and workflows including interactive rebasing, conflict resolution, history manipulation, bisecting for bugs, cherry-picking, reflog recovery, and branch management strategies. Use for: (1) Interactive rebasing and commit cleanup, (2) Complex merge conflict resolution, (3) Git bisect for bug hunting, (4) History rewriting and cleanup, (5) Branch strategy implementation (Git Flow, trunk-based), (6) Recovering lost commits with reflog |
Advanced Git Operations
Overview
Master advanced Git workflows for complex version control scenarios. This skill covers sophisticated operations beyond basic commits and merges, including interactive rebasing, advanced conflict resolution, history manipulation, and strategic branch management.
Use this skill when you need to:
- Clean up messy commit history before code review
- Resolve complex merge conflicts strategically
- Hunt down bugs with binary search (bisect)
- Recover lost commits or undo mistakes
- Implement team branching strategies
- Rewrite history safely
Core Capabilities
Interactive Rebasing
- Reorder commits for logical history
- Squash multiple commits into one
- Edit commit messages retroactively
- Split commits into smaller pieces
- Remove unwanted commits from history
Conflict Resolution
- Strategic merge conflict handling
- Three-way merge understanding
- Ours vs theirs strategy selection
- Conflict marker interpretation
- Partial file staging during conflicts
Git Bisect
- Binary search for bug introduction
- Automated bisect with scripts
- Good/bad commit identification
- Efficient debugging of regressions
History Manipulation
- Amend commits safely
- Filter-branch operations
- BFG Repo-Cleaner for large files
- Rewrite history with caution
- Preserve attribution and dates
Branch Strategies
- Git Flow workflow
- Trunk-based development
- Feature branch workflows
- Release branch management
- Hotfix procedures
Quick Command Reference
Interactive Rebase
git rebase -i HEAD~5
git rebase -i main
git rebase --continue
git rebase --abort
Conflict Resolution
git checkout --ours <file>
git checkout --theirs <file>
git diff --name-only --diff-filter=U
git add <file>
Git Bisect
git bisect start
git bisect bad
git bisect good <commit-hash>
git bisect run ./test-script.sh
git bisect reset
Cherry-Pick
git cherry-pick <commit-hash>
git cherry-pick -n <commit-hash>
git cherry-pick A^..B
Reflog Recovery
git reflog
git checkout -b recovery <commit-hash>
git reset --hard HEAD@{2}
Core Workflows
1. Interactive Rebase Workflow
When to Use:
- Clean up messy commit history before pushing
- Combine "WIP" or "fix typo" commits
- Reorder commits for logical progression
- Split large commits into focused changes
Steps:
git rebase -i HEAD~5
Rebase Commands:
pick (p): Keep commit as-is
reword (r): Keep commit, edit message
edit (e): Keep commit, stop to amend
squash (s): Combine with previous commit, keep message
fixup (f): Combine with previous commit, discard message
drop (d): Remove commit entirely
Example:
pick abc1234 Add feature X
pick def5678 Fix typo
pick ghi9012 WIP commit
pick jkl3456 Update documentation
pick abc1234 Add feature X
fixup def5678 Fix typo
drop ghi9012 WIP commit
reword jkl3456 Update documentation
Safety Tips:
- Never rebase commits already pushed to shared branches
- Create backup branch:
git branch backup
- Use
git reflog if something goes wrong
- Force push carefully:
git push --force-with-lease
For detailed examples and advanced techniques, see examples/interactive_rebase.md.
2. Conflict Resolution Workflow
Understanding Conflict Markers:
<<<<<<< HEAD (Current Change)
Your code from current branch
=======
Their code from merging branch
>>>>>>> branch-name (Incoming Change)
Resolution Process:
git status
vim <file>
git add <file>
git checkout --ours <file>
git checkout --theirs <file>
git add <file>
git mergetool
git merge --continue
git rebase --continue
Three-Way Diff:
git diff --ours <file>
git diff --theirs <file>
git diff --base <file>
For common conflict patterns and solutions, see examples/conflict_resolution.md.
3. Git Bisect for Bug Hunting
Scenario: Bug exists in current version, find which commit introduced it.
Manual Bisect:
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect bad
git bisect good
git bisect reset
Automated Bisect:
npm test
exit $?
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect run ./test.sh
git bisect reset
4. Branch Management
Quick Cleanup:
bash scripts/git_helper.sh cleanup-branches
git branch --merged main | grep -v "\*\|main\|develop" | xargs git branch -d
git fetch --prune
Stale Branch Detection:
for branch in $(git branch -r | grep -v HEAD); do
echo -e "$(git show --format="%ci %cr" $branch | head -n 1)\t$branch"
done | sort -r
For detailed branch management strategies, see references/branch-management.md.
5. Reflog Recovery
Recover Deleted Branch:
git reflog
git checkout -b feature-x <commit-hash>
Undo Bad Reset:
git reflog
git reset --hard HEAD@{1}
Recover Lost Commits:
git fsck --lost-found
git cherry-pick <lost-commit-hash>
For comprehensive recovery techniques, see references/reflog-recovery.md.
Branch Strategy Implementation
This skill supports implementing various branching strategies. Choose based on your team's needs:
Git Flow
Best for: Projects with scheduled releases, multiple production versions
Branch Types:
main: Production-ready code
develop: Integration branch
feature/*: New features
release/*: Release preparation
hotfix/*: Production fixes
Quick Start:
git checkout develop
git checkout -b feature/user-auth
git checkout develop
git merge --no-ff feature/user-auth
Trunk-Based Development
Best for: Continuous deployment, fast-moving teams
Principles:
- Single main branch
- Short-lived feature branches (< 1 day)
- Frequent integration
- Feature flags for incomplete work
Quick Start:
git checkout main
git checkout -b feature/quick-fix
git checkout main
git merge feature/quick-fix
For complete branch strategy workflows, see examples/branch_strategies.md.
Best Practices
Before Rebase
- Create backup branch:
git branch backup
- Ensure working directory is clean:
git status
- Know your commit history:
git log --oneline
- Never rebase public/shared branches
During Conflicts
- Understand both sides of the conflict
- Test thoroughly after resolution
- Use meaningful merge commit messages
- Document complex resolutions
Branch Management
- Delete merged branches promptly
- Use descriptive branch names:
feature/user-login
- Keep branches focused and short-lived
- Sync with main branch regularly
History Hygiene
- Squash "fixup" commits before pushing
- Write clear, descriptive commit messages
- Keep commits atomic (one logical change)
- Use conventional commit format when possible
For comprehensive best practices, see references/best-practices.md.
Troubleshooting
Rebase Conflicts
git rebase --abort
git merge --no-ff <branch>
Lost Commits
git reflog
git checkout -b recovery <commit-hash>
Detached HEAD State
git checkout -b new-branch-name
git checkout main
Failed Push After Rebase
git push --force-with-lease
git checkout -b feature-v2
git push origin feature-v2
For detailed troubleshooting, see references/troubleshooting.md.
Additional Resources
Detailed Guides
examples/interactive_rebase.md: Step-by-step rebase examples with scenarios
examples/conflict_resolution.md: Common conflict patterns and solutions
examples/branch_strategies.md: Complete Git Flow and trunk-based workflows
Reference Documentation
references/branch-management.md: Branch cleanup automation and strategies
references/reflog-recovery.md: Recovery techniques and history rewriting
references/best-practices.md: Comprehensive best practices and quality standards
references/troubleshooting.md: Common issues and emergency recovery
Helper Scripts
scripts/git_helper.sh: Branch cleanup and conflict resolution utilities
Quick Reference Commands
Must-Know Commands
git rebase -i HEAD~N
git rebase --abort
git merge --abort
git cherry-pick --abort
git log --oneline --graph --all
git reflog
git checkout --ours <file>
git checkout --theirs <file>
git fsck --lost-found
git reflog
git branch --merged | xargs git branch -d
git fetch --prune
Safety First
- Always backup before history rewrite:
git branch backup
- Never force push to shared branches: Use
--force-with-lease
- Test after conflicts: Don't assume resolution is correct
- Document complex operations: Leave comments in merge commits
- Use reflog: It's your safety net for 30+ days
Master these advanced Git operations to maintain clean history, resolve conflicts efficiently, and manage complex development workflows with confidence.