| name | git-branch-tracking |
| description | Guide and workflow for Git Branch Tracking & PR Workflow. Use when you need Git Branch Tracking & PR Workflow. |
Git Branch Tracking & PR Workflow
Purpose: Ensure proper remote branch tracking after creating PRs to avoid "no remote" and "no upstream" status issues.
🚨 Problem: Missing Remote Tracking
After creating a PR, the branch status may show:
Local: feature-branch (no remote) | Remote: no upstream
This happens when:
- Branch was created locally without
-u flag
- Push was done with
HEAD:branch-name instead of -u origin branch-name
- Tracking wasn't set up during initial push
✅ Solution: Always Set Upstream During Push
Method 1: Use -u Flag (Recommended)
When creating new branch and pushing:
git checkout -b feature-branch
git add .
git commit -m "Feature implementation"
git push -u origin feature-branch
When pushing existing branch:
git push -u origin HEAD
Method 2: Set Tracking After Push
If you already pushed without -u:
git branch -vv
git branch --set-upstream-to=origin/feature-branch feature-branch
git branch -vv
Method 3: Git Config Default
Make all pushes set tracking by default:
git config --global push.autoSetupRemote true
Now any git push will automatically set tracking!
🎯 Best Practices for PR Workflow
1. Create Branch and Push with Tracking
git checkout main
git pull origin main
git checkout -b feature/new-feature
git add .
git commit -m "Implement new feature"
git push -u origin feature/new-feature
2. Create PR Immediately After Push
gh pr create --title "Add new feature" --body "Description"
3. Verify Tracking Status
git branch -vv
🔧 Common Commands
Check Branch Info
git branch -vv
git branch -r
git branch -a
Fix Missing Tracking
git branch --set-upstream-to=origin/$(git branch --show-current)
git push -u origin HEAD
git config --global push.autoSetupRemote true
Update Remote References
git fetch origin
git fetch --prune
git remote prune origin
📋 Git Status Header Format
Correct format after proper setup:
[Local: feature-branch | Remote: origin/feature-branch | PR: 2048 https://...]
Incorrect format (missing tracking):
[Local: feature-branch (no remote) | Remote: no upstream | PR: N/A]
🚀 Automated PR Workflow
Recommended Script Pattern
#!/bin/bash
BRANCH_NAME="${1:-feature/$(date +%s)}"
PR_TITLE="$2"
PR_BODY="$3"
git checkout -b "$BRANCH_NAME"
git commit -m "$PR_TITLE"
git push -u origin "$BRANCH_NAME"
gh pr create --title "$PR_TITLE" --body "$PR_BODY" --base main
echo ""
echo "Branch tracking status:"
git branch -vv | grep "^\*"
Usage:
./create-pr.sh feature/add-validation "Add validation" "Implements input validation"
⚠️ Common Mistakes to Avoid
Mistake 1: Using HEAD:branch-name
git push origin HEAD:feature-branch
git push -u origin feature-branch
git push -u origin HEAD
Mistake 2: Forgetting -u Flag
git push origin feature-branch
git push -u origin feature-branch
Mistake 3: Creating PR Before Push
gh pr create ...
git push origin feature-branch
git push -u origin feature-branch
gh pr create ...
🔍 Troubleshooting
Issue: "no remote" in status
Cause: Branch not tracking any remote branch
Fix:
git branch --set-upstream-to=origin/$(git branch --show-current)
git push -u origin HEAD
Issue: "no upstream" in status
Cause: Same as "no remote" - missing tracking configuration
Fix: Same as above
Issue: Push rejected (branch exists)
Cause: Trying to push with -u but remote branch exists
Fix:
git push -u origin HEAD
git branch --set-upstream-to=origin/existing-branch
git pull
Issue: Can't find remote branch
Cause: Remote branch deleted or never existed
Fix:
git fetch origin
git branch -r | grep your-branch
git push -u origin your-branch
📚 Quick Reference
| Command | Purpose |
|---|
git push -u origin HEAD | Push and set tracking |
git branch -vv | Show tracking status |
git branch --set-upstream-to=origin/branch | Set tracking manually |
git config push.autoSetupRemote true | Auto-setup tracking (global) |
git fetch --prune | Update remote refs, remove deleted |
git remote -v | Show remote URLs |
🎯 Recommended Workflow
git checkout -b feature/my-feature
git add .
git commit -m "Implement feature"
git push -u origin feature/my-feature
gh pr create --title "Add feature" --body "Description"
git branch -vv
💡 Pro Tips
-
Use git aliases for common workflows:
git config --global alias.pushup '!git push -u origin HEAD'
git config --global alias.track '!git branch --set-upstream-to=origin/$(git branch --show-current)'
-
Enable auto-setup to never worry about -u:
git config --global push.autoSetupRemote true
-
Check tracking before PR:
git branch -vv | grep "^\*" | grep -q "\[origin/" || echo "⚠️ No tracking set!"
-
Include tracking in git status:
git status -sb
Last Updated: 2025-11-17
Applies To: Git 2.0+
Related: PR workflow, branch management, remote tracking