| name | git-workflow-mastery |
| description | Master Git workflows including branching strategies, interactive rebase, cherry-pick, bisect, worktrees, and advanced merge conflict resolution. Use when working with git workflow mastery. |
| domain | development |
| author | oyi77 |
| license | Apache-2.0 |
| subdomain | software-development |
| tags | ["git","version-control","branching","rebase","worktrees","merge"] |
| version | 1.0.0 |
Git Workflow Mastery
When to Use
Trigger phrases:
-
"git workflow mastery"
-
"Master Git workflows including branching strategies, interactive rebase, cherry-"
-
When setting up branching strategy for a team
-
When resolving complex merge conflicts
-
When bisecting to find bug-introducing commits
-
When managing multiple features in parallel with worktrees
-
When cleaning up commit history before a PR
-
When recovering from a broken Git state (detached HEAD, lost commits)
-
When setting up CI/CD pipeline triggers per branch
When NOT to Use
- For simple add-commit-push workflows
- When the team already has a working Git workflow
- When you only need to clone and pull — no branching or history manipulation
Overview
Advanced Git workflows for professional development teams. Covers Git Flow, GitHub Flow, trunk-based development, interactive rebase, worktrees, and conflict resolution. This skill assumes you already know git add, git commit, git push, and git pull. It covers the next tier: history manipulation, parallel workspace management, binary-search debugging, and safe collaboration patterns.
Git is a Directed Acyclic Graph (DAG) of commits. Understanding this — that branches are just pointers, that rebase rewrites topology, that the reflog tracks every pointer movement — is the foundation of mastery. Every operation in this skill builds on that mental model.
Branching Strategies
Git Flow (release-oriented)
main ───●──────●────────────●──────────
\ / /
develop ●──●──●──●──●──●──●──●
\ / \ /
feature/foo ●──● ●──●
\
release/v1.1 ●──●
Best for projects with scheduled releases and long-lived feature branches.
git flow init -d
git flow feature start user-auth
git flow feature finish user-auth
git flow release start v1.1.0
git flow release finish v1.1.0
git flow hotfix start 1.1.1
git flow hotfix finish 1.1.1
GitHub Flow (continuous deployment)
main ●──●──●──●──●──●──●──●──●
\ / \ /
feat ●──● ●──●
One permanent branch (main). Feature branches branch off, are reviewed via PR, and merge back. Deploy after every merge.
git checkout main
git pull origin main
git checkout -b feat/user-auth
git push -u origin feat/user-auth
git branch -d feat/user-auth
git fetch origin --prune
Trunk-Based Development (fast CI)
main ●──●──●──●──●──●──●──●──●──●
\/ \/
└─short-lived─┘
Short-lived feature branches (hours, not days). No branch lives longer than one sprint. Every commit to main is deployable.
git checkout -b fix/login-crash
git commit -m "fix: handle null session in login handler"
git push -u origin fix/login-crash
Branch naming conventions:
| Prefix | Purpose | Example |
|---|
feat/ | New feature | feat/user-auth |
fix/ | Bug fix | fix/login-crash |
chore/ | Maintenance | chore/upgrade-deps |
docs/ | Documentation | docs/api-readme |
refactor/ | Code restructuring | refactor/auth-module |
test/ | Adding tests | test/auth-flow |
perf/ | Performance | perf/query-cache |
release/ | Release prep | release/v1.1.0 |
Conventional Commits
<type>(<scope>): <description>
[optional body]
[optional footer(s)]
git commit -m "feat(auth): add OAuth2 login flow"
git commit -m "feat(api)!: change response format from XML to JSON"
git commit -m "fix(cache): evict stale entries on write
Previously the cache only evicted on TTL expiry.
Now it evicts the stale key on every write to prevent
serving outdated data during high-throughput writes.
Closes #142"
Interactive Rebase
Interactive rebase rewrites commit history by reordering, squashing, fixing up, dropping, or rewording commits. Use it before opening a PR to present a clean, logical history.
Basic Operations
git rebase -i HEAD~3
Rebase commands:
| Command | Short | Effect |
|---|
pick | p | Use commit as-is |
reword | r | Edit commit message only |
squash | s | Combine with previous commit, keep both messages |
fixup | f | Combine with previous commit, discard message |
drop | d | Remove commit entirely |
edit | e | Stop to amend commit content |
Squash Worked Commits
git merge-base HEAD main
git rebase -i main
git add <resolved-file>
git rebase --continue
Split a Commit
git rebase -i HEAD~3
git reset HEAD^
git add src/auth/
git commit -m "feat(auth): add login handler"
git add src/db/
git commit -m "feat(db): add users migration"
git add tests/
git commit -m "test(auth): add login flow tests"
git rebase --continue
Reorder Commits
Rebase onto a Different Branch
git checkout feat/user-auth
git rebase main
git rebase --onto main base-branch feat/user-auth
Edit the Root Commit
git rebase -i --root
Cherry-Pick
Apply specific commits from one branch to another without merging the full history.
Basic Cherry-Pick
git checkout release/v1.0
git cherry-pick abc123
git cherry-pick abc123..def456
git cherry-pick feature/new-api -- src/api/handler.ts
Cherry-Pick Options
git cherry-pick -n abc123
git cherry-pick -e abc123
git cherry-pick -x abc123
git cherry-pick --no-commit-date abc123
Cherry-Pick with Conflicts
git cherry-pick abc123
git add src/app.ts
git cherry-pick --continue
git cherry-pick --abort
Cherry-Pick Strategy for Hotfix Backport
git checkout main
git commit -m "fix: resolve payment race condition"
HASH=$(git rev-parse HEAD)
git checkout release/v1.0
git cherry-pick "$HASH"
Cherry-Pick a Branch onto Another
git cherry-pick main..feature-branch
Bisect
Binary-search through history to find the exact commit that introduced a bug. Works in O(log n) time — 10 steps for 1000 commits.
Manual Bisect
git bisect start
git bisect bad
git bisect good v1.0
git bisect good
git bisect bad
Scripted Bisect (Fully Automated)
Write a test script that exits 0 (good) or non-zero (bad):
cat > /tmp/test-bug.sh << 'EOF'
npm run build
npm test -- --grep "login should fail with invalid token"
EOF
chmod +x /tmp/test-bug.sh
git bisect start HEAD v1.0
git bisect run /tmp/test-bug.sh
Bisect with Skip (Flaky Tests)
git bisect start HEAD v1.0
git bisect run /tmp/test-bug.sh
git bisect skip
Bisect with Logging
git bisect start HEAD v1.0
git bisect run sh -c "npm run build && npm test 2>&1 | tee /tmp/bisect-log.txt"
Bisect Reset
git bisect reset
Worktrees
Git worktrees allow checking out multiple branches simultaneously in separate directories, all sharing the same Git repository.
Basic Worktree Operations
git worktree add ../feat/user-auth -b feat/user-auth
git worktree add ../fix/crash fix/login-crash
git worktree list
Worktree Lifecycle
git worktree add ../debug/deploy-tag v1.0.0
git worktree add --lock ../release/v1.1 release/v1.1
git worktree remove ../feat/user-auth
git worktree remove --force ../release/v1.1
git worktree prune
Worktree for Code Review
git worktree add ../review/pr-42 feature/pr-42
cd ../review/pr-42
npm install
npm test
cd /repo/main
git worktree remove ../review/pr-42
Worktree for Emergency Hotfix
git worktree add ../hotfix/crash main
cd ../hotfix/crash
git checkout -b hotfix/payment-null
git add .
git commit -m "fix: handle null payment amount"
git push -u origin hotfix/payment-null
cd /repo/main
git worktree remove ../hotfix/crash
Worktree with .gitignore Safety
echo ".worktrees/" >> .gitignore
git add .gitignore
git commit -m "chore: ignore worktree directory"
git worktree add .worktrees/my-feature -b feat/new-feature
Common Issues & Troubleshooting
Detached HEAD State
What happened: You checked out a commit hash instead of a branch name. HEAD points directly to a commit, not a branch reference.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches...
Recovery scenarios:
git checkout main
git checkout -b new-branch-name
git checkout existing-branch
git cherry-pick detached-branch..HEAD
git checkout main
Complex Merge Conflict Resolution
Step-by-step for nasty conflicts:
git rebase main
git mergetool
git add src/config.ts
git rebase --continue
git rebase --abort
Conflict patterns and resolutions:
| Conflict Pattern | Strategy |
|---|
| Both sides added the same function | Compare implementations, keep the correct one |
| One side deleted, other modified | git checkout --ours/--theirs src/file.ts to pick |
| Whitespace/formatting only | git rebase -X theirs to auto-resolve with incoming |
| Binary file conflict | Pick one side: git checkout --theirs logo.png |
| Rename/add conflict | Manually reconcile the rename with the new file |
| Multiple files, same pattern | Use a script to batch-resolve known-safe patterns |
git checkout --ours src/config.ts
git add src/config.ts
git diff --name-only --diff-filter=U | xargs git checkout --ours
git add -u
Reflog Recovery (Lost Commits)
When you need it: After a bad rebase, accidental branch delete, or git reset --hard that went too far.
git reflog
git reset --hard HEAD@{2}
git branch recover-branch HEAD@{3}
git reflog show feat/auth
git reflog --date=relative
Force Push Safety
git push --force-with-lease origin feat/auth
git push --force-with-lease=feat/auth:origin/feat/auth
git push --force origin feat/auth
Lost Work After Stash Drop
git fsck --unreachable | grep commit | cut -d' ' -f3 | xargs git log --mergeless --oneline
gitk --all $(git fsck --unreachable | grep commit | cut -d' ' -f3)
git branch recover-stash abc1234
Undoing Things
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git revert HEAD
git revert abc1234
git add forgotten-file.ts
git commit --amend --no-edit
git commit --amend -m "fix: better commit message"
Cleanup and Optimization
git fetch --prune
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d
git rebase -i --autosquash main
git gc --aggressive --prune=now
git clean -fd
git clean -fdn
Red Flags
| Situation | Risk | Action |
|---|
| Force pushing to shared branches | Destroys collaborators' history | Use --force-with-lease or never force push |
| Rebasing a branch others have pulled | Divergent histories, confusion | Communicate before force-push; coordinate pull timing |
| Interactive rebase on published commits | Rewriting public history | Only rebase unpublished commits |
Cherry-pick without -x on hotfix branches | No traceability back to source | Use -x to annotate cherry-picks |
| Merge commits in a feature branch | Cluttered history before review | Squash or rebase before PR |
| Working with dirty working tree | Accidental commit of unrelated changes | Commit or stash before switching context |
| Long-lived feature branches | Merge hell, integration pain | Keep branches <1 sprint; rebase daily |
Not running git worktree prune after manual delete | Stale worktree references | Prune after removing worktree directories |
Using git reset --hard without checking git status | Losing uncommitted work | Always git stash or check status first |
Monetization
This skill generates income through the following channels:
1. Git Workflow Consulting ($150-400/hr)
Companies adopting Git or migrating from centralized VCS (SVN, TFS, Perforce) need workflow setup and team training.
Services:
- Branch strategy design and CONTRIBUTING.md documentation
- CI/CD trigger setup per branch strategy
- Migration from SVN/TFS to Git with history preservation
- Team training workshop (half-day or full-day)
- Code review culture implementation using GitHub/GitLab flows
Outreach: Target startups scaling from 5→20+ engineers (the point where Git chaos sets in).
2. Automated Git Audit Tool ($500-2,000/project)
Build a CLI tool that scans a repo and reports:
- Branch naming convention violations
- Merge commit frequency in feature branches
- Commit message quality (Conventional Commits compliance)
- Stale branch age and count
- Large file tracking and BFG cleanup candidates
$ git-audit .
❌ Branch naming: 3 branches don't match convention (fix/ vs fix-)
❌ Merge commits: 12 merge commits in feature branches
⚠️ Commit quality: 40% pass Conventional Commits
ℹ️ Stale branches: 8 branches untouched >30 days
ℹ️ Large files: 2 files >10MB should use Git LFS
3. Emergency Git Recovery Service ($100-500/incident)
Developers frequently lose work through bad rebases, force pushes, or accidental branch deletion. Offer a recovery service:
ssh client-server
cd /repo
git reflog
git branch rescue-branch HEAD@{5}
git format-patch main..rescue-branch --stdout > recovery.patch
Package this as an automated CLI tool + premium human-assisted recovery.
4. Git Automation Scripts / SaaS ($10-50/month per seat)
Build scripts that automate common complex workflows:
git-auto-release --type minor --message "release: v1.2.0"
git-rebase-all
git-bulk-squash
Sell as npm package or marketplace extension (GitHub Actions, GitLab CI templates).
5. Training Content ($27-297/course)
- "Git Mastery for Teams" — video course (6 modules, 3 hours)
- "Git Recovery Playbook" — PDF guide with 20 disaster-recovery scenarios
- "Git Workflow Templates" — reusable branching docs + hooks scripts
6. Internal Adoption for Your Team
Directly reduces integration time, CI pipeline failures, and onboarding overhead:
- Measured impact: Teams adopting Git Flow or trunk-based development reduce merge-conflict resolution time by 60-80%
- Onboarding: New engineers reach shipping velocity 2-3x faster with documented conventions
- CI reliability: Clean history means cleaner CI triggers — fewer false-positive failures
Verification
Process
- Prepare — Gather requirements, verify prerequisites, set up environment
- Choose strategy — Select branching model based on release cadence (Git Flow, GitHub Flow, trunk-based)
- Branch naming — Use convention: feat/, fix/, chore/, docs/, refactor/, test/, perf/
- Commit messages — Follow Conventional Commits format with scope and body
- Interactive rebase — Clean up history before merge using squash, fixup, reword
- Cherry-pick — Apply specific commits to other branches for hotfix backport
- Bisect — Binary search for bug-introducing commits using manual or scripted mode
- Worktrees — Parallel work on multiple branches with lifecycle management
- Verify — Validate output meets requirements, document results, clean up
Anti-Rationalization Table
| Rationalization | Reality |
|---|
| "I will clean up commits later" | You never do. Interactive rebase before every PR. |
| "Force push is fine on my branch" | Force push destroys history. Use --force-with-lease if you must. |
| "Merge commits are fine" | Squash or rebase keeps history linear and readable |
| "Git bisect is overkill" | It finds the exact bug-introducing commit in O(log n) time |
| "I can just reset --hard and redo it" | You lose uncommitted work and the reflog entry might be your only lifeline |
| "The merge conflict is too complex, I'll start over" | Conflict resolution is a skill. Use mergetool, learn the patterns, persist. |
| "Worktrees are just for large projects" | Any project with context-switching benefits from isolated workspaces |
| "Reflog is only for emergencies" | Check reflog regularly — it's the best undo button you have |