| name | tools-git-advanced |
| description | Advanced git operations including rebase, cherry-pick, bisect, reflog, stash, and history manipulation for complex workflows. |
Advanced Git Operations
Overview
Advanced git commands for complex workflows, history manipulation, and debugging. Use this skill for rebasing, cherry-picking, finding bugs, and recovering from mistakes.
When to Use
- Rebasing branches and cleaning history
- Cherry-picking specific commits
- Finding which commit introduced a bug
- Recovering lost commits
- Complex merge scenarios
- Stash management
Interactive Rebase
Start Rebase
git rebase -i HEAD~5
git rebase -i main
git rebase -i --root
Rebase Commands
| Command | Action |
|---|
pick | Keep commit as-is |
reword | Change commit message |
edit | Stop to amend commit |
squash | Combine with previous |
fixup | Combine, discard message |
drop | Remove commit |
exec | Run shell command |
Common Rebase Operations
git rebase -i HEAD~3
git rebase -i HEAD~5
git rebase -i HEAD~3
git rebase -i HEAD~3
git reset HEAD~
git add -p && git commit
git add -p && git commit
git rebase --continue
Rebase onto Branch
git rebase main
git rebase main feature
git rebase --onto main A B
Abort/Continue
git rebase --continue
git rebase --skip
git rebase --abort
Cherry-Pick
git cherry-pick <commit>
git cherry-pick A B C
git cherry-pick A..B
git cherry-pick A^..B
git cherry-pick -n <commit>
git cherry-pick -x <commit>
git cherry-pick --abort
git cherry-pick --continue
Cherry-Pick from Another Branch
git log other-branch --oneline
git cherry-pick abc123
Git Bisect (Find Bug)
Basic Bisect
git bisect start
git bisect bad
git bisect good v1.0
git bisect good
git bisect bad
git bisect reset
Automated Bisect
git bisect start HEAD v1.0
git bisect run npm test
git bisect run ./test-script.sh
Bisect Log
git bisect log
git bisect log > bisect.log
git bisect replay bisect.log
Reflog (Recovery)
View Reflog
git reflog
git reflog show main
git reflog --date=relative
Recover Lost Commits
git reflog
git reset --hard HEAD@{2}
git reflog
git checkout -b recovered HEAD@{5}
git fsck --unreachable | grep commit
git show <commit>
Stash Management
Basic Stash
git stash
git stash -u
git stash -a
git stash push -m "message"
git stash push path/to/file
List & Apply
git stash list
git stash show
git stash show -p stash@{1}
git stash apply
git stash apply stash@{2}
git stash pop
git stash drop stash@{1}
git stash clear
Branch from Stash
git stash branch newbranch
Reset vs Revert
Reset (Rewrite History)
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git reset --hard origin/main
Revert (Safe, Creates Commit)
git revert <commit>
git revert HEAD~3..HEAD
git revert -n <commit>
git revert -m 1 <merge-commit>
Amending
git commit --amend
git commit --amend -m "new msg"
git commit --amend --no-edit
git commit --amend --author="Name <email>"
Clean & Prune
git clean -n
git clean -f
git clean -fd
git clean -fX
git gc
git prune
Worktrees
git worktree list
git worktree add ../feature feature-branch
git worktree add ../hotfix -b hotfix
git worktree remove ../feature
git worktree prune
Blame & Log
git blame <file>
git blame -L 10,20 <file>
git log -p <file>
git log --follow <file>
git log -S "searchterm"
git log -G "regex"
git log --all --oneline --graph
Patches
git format-patch HEAD~3
git format-patch main..feature
git apply patch.patch
git am patch.patch
Troubleshooting
| Scenario | Solution |
|---|
| Undo last commit | git reset --soft HEAD~1 |
| Undo pushed commit | git revert <commit> |
| Wrong branch commit | Cherry-pick to correct branch |
| Lost commits | git reflog to find and recover |
| Bad rebase | git rebase --abort or reflog |
| Merge conflict hell | git merge --abort and retry |
| Detached HEAD | git checkout -b newbranch |