| name | git-specialist |
| description | Deep Git command-line expertise covering the full spectrum from everyday workflows to advanced power-user techniques. Use when the user needs help with: Git commands, branching strategies, rebasing, cherry-picking, interactive rebase, reflog recovery, bisect debugging, worktrees, subtrees, submodules, patch workflows, advanced log/diff/blame, stash tricks, hooks, aliases, large-repo performance, conflict resolution, history rewriting, Git internals, .gitattributes/.gitignore patterns, sparse checkout, or any Git CLI question. Triggers on: git, rebase, cherry-pick, merge conflict, reflog, bisect, worktree, stash, reset, revert, amend, squash, fixup, force push, git log, git diff, git blame, git hook, .gitignore, git alias, git subtree, git submodule, sparse-checkout, git filter-repo, git rerere, git notes, git bundle, git archive. |
Git Specialist
Complete Git CLI mastery -- from daily workflows to deep internals.
Workflow
- Identify the scenario -- what is the user trying to achieve (recovery, history rewrite, workflow optimization, debugging, performance)?
- Choose the safest approach -- prefer non-destructive commands; warn before any destructive operation (
--force, filter-repo, reset --hard)
- Explain with concrete examples -- show exact commands with realistic output
- Offer alternatives -- show both the quick way and the "proper" way when they differ
Safety Rules
- Always warn before history-rewriting commands on shared branches
- Prefer
--force-with-lease over --force
- Suggest creating a backup branch before destructive operations:
git branch backup/before-rewrite
- Recommend
git stash or git worktree before risky operations
Core Topics Quick Reference
| Area | Key Commands | Notes |
|---|
| Branching | branch, switch, checkout -b | Prefer switch/restore over checkout (Git 2.23+) |
| History | log, reflog, shortlog, log --graph | reflog is the safety net -- it tracks HEAD movements for ~90 days |
| Rewriting | rebase -i, commit --amend, filter-repo | Interactive rebase is the Swiss army knife for local history |
| Merging | merge, rebase, cherry-pick | Know when each is appropriate; prefer rebase for linear history on feature branches |
| Recovery | reflog, fsck, cherry-pick, reset | Almost nothing is truly lost if it was committed |
| Debugging | bisect, blame, log -S, log -G | bisect automates binary search for regressions |
| Stash | stash push, stash pop, stash apply | Use -m "message" and --include-untracked |
| Worktrees | worktree add, worktree list | Multiple working directories from one repo -- no more stash juggling |
| Patches | format-patch, am, apply, diff | Email-based workflow; also useful for selective transfers |
| Performance | sparse-checkout, shallow clone, partial clone, maintenance | Essential for large monorepos |
| Hooks | pre-commit, commit-msg, pre-push | Automate quality gates; use core.hooksPath for shared hooks |
| Config | config, alias, .gitattributes | Three scopes: --system, --global, --local |
| Subprojects | submodule, subtree | Subtree is simpler; submodule gives exact version pinning |
| Internals | cat-file, rev-parse, ls-tree, hash-object | Understand the object model: blob, tree, commit, tag |
Power-User Tricks
History Navigation & Search
git log -S "functionName" --oneline
git log -G "regex_pattern" --oneline
git log -L :functionName:path/to/file.cs
git log --graph --oneline --decorate --all --date=relative --format="%C(yellow)%h%Creset %C(cyan)%ad%Creset %s %C(green)(%an)%Creset"
git log --all --grep="Co-authored-by:.*Name"
git log main..branch-a --oneline
git log --ancestry-path --merges <commit>..main | tail -1
Interactive Rebase Mastery
git rebase -i HEAD~5
git commit --fixup=<target-sha>
git rebase -i --autosquash main
git rebase -i --root
git rebase --onto new-base old-base feature-branch
git rebase --abort
Recovery & Undo
git reset --soft HEAD~1
git reset --mixed HEAD~1
git fsck --unreachable | grep commit | cut -d' ' -f3 | xargs git show --stat
git reflog | grep "branch-name"
git checkout -b branch-name <sha-from-reflog>
git reflog
git reset --hard HEAD@{n}
git revert -m 1 <merge-commit-sha>
git restore --source=<commit> -- path/to/file
Stash Power Moves
git stash push -p -m "partial stash"
git stash push --include-untracked -m "with untracked"
git stash branch new-branch-name stash@{0}
git stash show -p stash@{0}
git stash clear
Worktree Workflow
git worktree add ../hotfix-branch hotfix/issue-123
git worktree list
git worktree remove ../hotfix-branch
git clone --bare repo.git .bare
echo "gitdir: ./.bare" > .git
git worktree add main
git worktree add develop
Bisect (Automated Bug Hunting)
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect good
git bisect start HEAD v1.0.0
git bisect run dotnet test --filter "TestName"
git bisect run bash -c 'grep -q "expected" output.txt'
git bisect reset
Advanced Diff & Blame
git diff --word-diff
git diff main..feature --name-only
git diff --cached
git diff -w
git blame -L 10,20 path/to/file
git blame -w -M -C path/to/file
echo "<formatting-sha>" > .git-blame-ignore-revs
git config blame.ignoreRevsFile .git-blame-ignore-revs
git blame path/to/file
Aliases (Productivity Boosters)
git config --global alias.st "status -sb"
git config --global alias.co "checkout"
git config --global alias.sw "switch"
git config --global alias.last "log -1 --stat"
git config --global alias.unstage "restore --staged"
git config --global alias.graph "log --graph --oneline --decorate --all"
git config --global alias.amend "commit --amend --no-edit"
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.wip "!git add -A && git commit -m 'WIP [skip ci]'"
git config --global alias.unwip "reset --soft HEAD~1"
git config --global alias.cleanup "!git branch --merged main | grep -v main | xargs -r git branch -d"
git config --global alias.fresh "!git fetch --prune && git rebase origin/main"
git config --global alias.who "shortlog -sne"
git config --global alias.find "log --all -S"
Large Repo Performance
git sparse-checkout init --cone
git sparse-checkout set src/my-service tests/my-service
git clone --filter=blob:none <url>
git clone --depth=1 <url>
git fetch --deepen=10
git config core.fsmonitor true
git config core.untrackedcache true
git maintenance start
git commit-graph write --reachable
Patches & Selective Transfer
git format-patch main..feature -o patches/
git am patches/*.patch
git diff main..feature -- src/ | git apply
git bundle create repo.bundle --all
git clone repo.bundle restored-repo
git archive --format=zip -o release.zip HEAD src/
Hooks & Automation
git config core.hooksPath .githooks
git diff --cached --name-only --diff-filter=d | grep '\.cs$' | xargs dotnet format --include
if ! grep -qE "^(feat|fix|build|chore|docs|style|refactor|test)\(.+\): .+" "$1"; then
echo "Commit message must follow: type(scope): description"
exit 1
fi
Rerere (Reuse Recorded Resolution)
git config --global rerere.enabled true
git rerere status
git rerere diff
Anti-Patterns
| Anti-Pattern | Do Instead |
|---|
git push --force on shared branches | git push --force-with-lease or git revert |
git add . blindly | git add -p or review with git diff first |
| Giant monolithic commits | Small, atomic commits with clear messages |
| Merging main into feature branch repeatedly | Rebase feature branch onto main |
| Deleting branches without checking merge status | git branch -d (safe) not -D (force) |
Using git checkout for everything | git switch (branches) + git restore (files) |
| Storing secrets in Git history | Use .gitignore, git-crypt, or environment variables |
git reset --hard without backup | Create a backup branch first, or use reflog |
Ignoring .gitattributes | Set text=auto, define merge drivers for lock files |
| Manual conflict resolution every time | Enable rerere for repeated resolutions |
Troubleshooting Checklist
| Symptom | Likely Cause | Fix |
|---|
| "detached HEAD" | Checked out a commit/tag directly | git switch - or git switch branch-name |
| Merge conflicts on every rebase | Diverged significantly from base | Rebase in smaller steps or use rerere |
| "refusing to merge unrelated histories" | Repos with no common ancestor | --allow-unrelated-histories if intentional |
| Push rejected (non-fast-forward) | Remote has commits you don't have | git pull --rebase then push |
.gitignore not working | File already tracked | git rm --cached <file> then commit |
| Huge repo, slow operations | Too many files/large blobs | sparse-checkout, partial clone, git-lfs |
| Lost commits after rebase | History rewritten | git reflog to find pre-rebase SHA |
| Wrong author on commits | Misconfigured user | git commit --amend --author="Name <email>" or rebase -i with exec |
| Binary files causing merge conflicts | No merge driver defined | Set binary merge strategy in .gitattributes |
| Submodule not updating | Submodule pointer stale | git submodule update --init --recursive |
Advanced Reference
For in-depth coverage of specific topics, load references/advanced-topics.md. Topics include:
- Git internals (object model, packfiles, refs)
git filter-repo for history surgery
- Signing commits and tags with GPG/SSH
- Git LFS setup and migration
- Monorepo strategies (sparse-checkout, worktrees, CODEOWNERS)
- Advanced merge strategies and custom merge drivers
- Git notes for metadata without changing history
- Shallow clone workflows for CI/CD
- Multi-remote workflows (fork + upstream)