| created | "2025-12-16T00:00:00.000Z" |
| modified | "2025-12-16T00:00:00.000Z" |
| reviewed | "2025-12-16T00:00:00.000Z" |
| name | git-branch-pr-workflow |
| description | Branch management, pull request workflows, and GitHub integration. Main-branch
development pattern (push main to remote feature branches), modern Git commands
(switch, restore), branch naming conventions, linear history, and GitHub MCP tools.
Use when user mentions creating branches, opening PRs, git switch, git restore,
feature branches, pull requests, or GitHub PR workflows.
|
| allowed-tools | Bash, Read, mcp__github__create_pull_request, mcp__github__list_pull_requests, mcp__github__update_pull_request |
Git Branch PR Workflow
Expert guidance for branch management, pull request workflows, and GitHub integration using modern Git commands and linear history practices.
Core Expertise
- Main-Branch Development: Work on main locally, push to remote feature branches for PRs
- Modern Git Commands: Use
git switch and git restore instead of checkout
- Branch Naming: Structured conventions (feat/, fix/, chore/, hotfix/)
- Linear History: Rebase-first workflow, squash merging, clean history
- GitHub MCP Integration: Use mcp__github__* tools instead of gh CLI
Main-Branch Development (Preferred)
Develop directly on main, push to remote feature branches for PRs. This eliminates local branch management overhead.
Basic Workflow
git switch main
git pull origin main
git add file.ts
git commit -m "feat(auth): add OAuth2 support"
git push origin main:feat/auth-oauth2
Multi-PR Workflow (Sequential Commits)
When you have commits for multiple PRs on main, push specific commit ranges to different remote branches:
git push origin abc1234^..def5678:feat/auth-oauth2
git push origin ghi9012^..ghi9012:fix/api-timeout
git push origin def5678..HEAD:fix/api-timeout
Commit range patterns:
git push origin <start>^..<end>:<remote-branch> - Push commit range (inclusive)
git push origin <commit>..<commit>:<remote-branch> - Push range (exclusive start)
git push origin <commit>..HEAD:<remote-branch> - Push from commit to current HEAD
git push origin main:<remote-branch> - Push entire main to remote branch
Benefits
- No local branch juggling - Always on main
- Always on latest main - No branch drift
- Clean local state - No stale branches to clean up
- Remote branches are ephemeral - Deleted after PR merge
- Simpler mental model - One local branch, many remote targets
Modern Git Commands (2025)
Switch vs Checkout
Modern Git uses specialized commands instead of multi-purpose git checkout:
git switch feature-branch
git switch -c new-feature
git switch -
git switch -c feature --track origin/feature
git switch -C force-recreate-branch
Restore vs Reset/Checkout
File restoration is now handled by git restore:
git restore --staged file.txt
git restore --staged .
git restore file.txt
git restore .
git restore --source=HEAD~2 file.txt
git restore --source=main --staged .
Command Migration Guide
| Legacy Command | Modern Alternative | Purpose |
|---|
git checkout branch | git switch branch | Switch branches |
git checkout -b new | git switch -c new | Create & switch |
git checkout -- file | git restore file | Discard changes |
git reset HEAD file | git restore --staged file | Unstage file |
git checkout HEAD~1 -- file | git restore --source=HEAD~1 file | Restore from commit |
Branch Naming Conventions
Structured Branch Names
git switch -c feat/payment-integration
git switch -c feat/user-dashboard
git switch -c feat/api-v2
git switch -c fix/login-validation
git switch -c fix/memory-leak-auth
git switch -c fix/broken-tests
git switch -c chore/update-dependencies
git switch -c chore/cleanup-tests
git switch -c refactor/auth-service
git switch -c hotfix/security-patch
git switch -c hotfix/critical-bug-fix
Branch Naming Format
{type}/{description}-{YYYYMMDD} (date optional but recommended for clarity)
Types:
feat/ - New features
fix/ - Bug fixes
chore/ - Maintenance, dependencies, linter fixes
docs/ - Documentation changes
refactor/ - Code restructuring
hotfix/ - Emergency production fixes
Linear History Workflow
Trunk-Based Development
Preferred: Main-branch development (see above) - no local feature branches needed.
Alternative: Local feature branches for complex multi-day work:
git switch main
git pull origin main
git switch -c feat/user-auth
git switch main && git pull
git switch feat/user-auth
git rebase main
git rebase -i main
git push -u origin feat/user-auth
Use local branches only when:
- Multi-day complex features requiring isolation
- Experimental work that might be abandoned
- Need to switch contexts frequently between unrelated work
Squash Merge Strategy
Maintain linear main branch history:
git switch main
git merge --squash feat/user-auth
git commit -m "feat: add user authentication system
- Implement JWT token validation
- Add login/logout endpoints
- Create user session management
Closes #123"
Interactive Rebase Workflow
Clean up commits before sharing:
git rebase -i HEAD~3
pick a1b2c3d feat: add login form
fixup d4e5f6g fix typo in login form
squash g7h8i9j add form validation
reword j1k2l3m implement JWT tokens
GitHub MCP Integration
Use GitHub MCP tools for all GitHub operations:
mcp__github__get_me()
mcp__github__list_pull_requests(owner="owner", repo="repo")
mcp__github__create_pull_request(
owner="owner",
repo="repo",
title="feat: add authentication",
head="feat/auth",
base="main",
body="## Summary\n- JWT authentication\n- OAuth support\n\nCloses #123"
)
mcp__github__update_pull_request(
owner="owner",
repo="repo",
pullNumber=42,
title="Updated title",
state="open"
)
mcp__github__list_issues(owner="owner", repo="repo")
Best Practices
Daily Integration Workflow
git switch main
git pull origin main
git switch feat/current-work
git rebase main
git add . && git commit -m "wip: daily progress checkpoint"
git push origin feat/current-work
git rebase -i main
git push --force-with-lease origin feat/current-work
Conflict Resolution with Rebase
git rebase main
git add resolved-file.txt
git rebase --continue
git rebase --abort
git merge main
Safe Force Pushing
git push --force-with-lease origin feat/branch-name
git config alias.pushf 'push --force-with-lease'
Main Branch Protection
Configure branch rules for linear history via GitHub MCP:
Pull Request Workflow
PR Title Format
Use conventional commit format in PR titles:
feat: add user authentication
fix: resolve login validation bug
docs: update API documentation
chore: update dependencies
PR Body Template
## Summary
Brief description of changes
## Changes
- Bullet points of key changes
- Link related work
## Testing
How changes were tested
## Issue References
<!-- Use GitHub autolink format - ALWAYS include relevant issues -->
Closes #123
<!-- Or use: Fixes #N, Resolves #N, Refs #N -->
Issue Reference Guidelines:
- Use
Closes #N / Fixes #N / Resolves #N to auto-close issues on merge
- Use
Refs #N / Related to #N for context without auto-closing
- Cross-repo:
Fixes owner/repo#N
- Multiple:
Fixes #1, fixes #2, fixes #3 (repeat keyword)
PR Creation Best Practices
- One focus per PR - Single logical change
- Small PRs - Easier to review (< 400 lines preferred)
- ALWAYS link issues - Use GitHub autolink format for traceability:
- Closing keywords:
Closes #123, Fixes #456, Resolves #789
- Reference without closing:
Refs #234, Related to #567
- Cross-repository:
Fixes owner/repo#123
- Multiple issues:
Fixes #1, fixes #2 (repeat keyword for each)
- Add labels - Use GitHub labels for categorization
- Request reviewers - Tag specific reviewers when needed
Troubleshooting
Branch Diverged from Remote
git pull --rebase origin feat/branch-name
git fetch origin
git reset --hard origin/feat/branch-name
Committed to Main (Expected Workflow)
With main-branch development, committing to main is the expected workflow:
git push origin main:feat/new-feature
git pull origin main
Why this works:
- Commits exist on both local main and remote feature branch
- When PR merges to remote main, your local main is behind by same commits
git pull recognizes the commits and fast-forwards cleanly
- No history rewriting, no data loss, no merge conflicts
Rebase Conflicts Are Too Complex
git rebase --abort
git merge main
Safe Operations
Recognizing Normal States
These states are expected during development - proceed confidently:
| State | Meaning | Action |
|---|
| Unstaged changes after pre-commit | Formatters modified files | Stage with git add -u and continue |
| Modified files after running formatters | Expected auto-fix behavior | Stage before committing |
| Pre-commit exit code 1 | Files were modified | Stage modifications, re-run pre-commit |
| Branch behind remote | Remote has newer commits | Pull or rebase as appropriate |
Confirmation-Required Commands
Request user confirmation before running destructive commands:
git branch -d/-D
git push origin --delete
git reset --hard
git clean -fd
When State is Unclear
When encountering unexpected state:
- Run diagnostic commands (
git status, git log --oneline -5)
- Report findings clearly
- Present options and wait for guidance
Recovery Workflows
Pre-commit Modifies Files
This is normal formatter/linter behavior:
git status
git add -u
git commit -m "feat(feature): description"
Push Rejected (Non-Fast-Forward)
Remote has newer commits:
git pull --rebase origin <branch>
git pull origin <branch>
git push --force-with-lease
Commit Fails
- Read the error message
- Common causes:
- Pre-commit hooks failed → Fix issues and retry
- No staged changes → Stage files first
- Empty commit message → Provide message
- Fix the underlying issue and retry