Skip to main content
3 min read Beginner

help

Git Branch Management Cheat Sheet


View Branches

TaskCommand
Current branchgit branch
All local branchesgit branch
Remote branchesgit branch -r
Local + Remotegit branch -a
Detailed statusgit branch -vv

Sync With GitHub

TaskCommand
Fetch updatesgit fetch
Fetch + remove deleted remote branchesgit fetch --prune
delete the branch from GitHub (remote)git push origin --delete feature-test
Pull latest changesgit pull
Update main branchgit checkout main && git pull

Create Branches

TaskCommand
Create branchgit branch feature-x
Create and switchgit checkout -b feature-x
(New syntax)git switch -c feature-x

Switch Branches

TaskCommand
Switch branchgit checkout branch-name
New syntaxgit switch branch-name

Push Branches

TaskCommand
Push current branchgit push
Push new branchgit push -u origin branch-name
Update remotegit push

Delete Branches

Local

Local Branch Management

git branch | grep "feature-" | xargs git branch -D

TaskCommand
Safe delete branchgit branch -d <branch-name>
Force delete branchgit branch -D <branch-name>
Delete all feature branchesgit branch | grep "^ feature-|^\* feature-" | xargs git branch -D
List local branchesgit branch
List merged branchesgit branch --merged main

Remote (GitHub)

TaskCommand
Delete remote branchgit push origin --delete branch-name

Example:

git push origin --delete feature-20260606-050823

Find Branches Already Merged

TaskCommand
Show merged branchesgit branch --merged
Show unmerged branchesgit branch --no-merged

Delete merged branches:

git branch --merged
git branch -d branch-name

Clean Up Local Repo After GitHub Deletions from Web

git fetch --prune
git branch -vv

Look for:

feature-x abc123 [origin/feature-x: gone]

Then:

git branch -D feature-x

Check Why GitHub Shows "Compare & Pull Request"

See commits not in main

git log --oneline main..feature-branch

Remote:

git log --oneline main..origin/feature-branch

Compare differences

git diff main..feature-branch

Reset Local Main to Match GitHub

⚠️ Discards local commits.

git checkout main
git fetch origin
git reset --hard origin/main

Useful Daily Workflow

Start work

git checkout main
git pull
git checkout -b feature-test

Commit changes

git add .
git commit -m "message"
git push -u origin feature-test

After PR merged

git checkout main
git pull

git branch -d feature-test
git push origin --delete feature-test

git fetch --prune

Debugging Checklist

QuestionCommand
Where am I?git branch
What's changed?git status
What branches exist?git branch -a
What is tracking what?git branch -vv
What's on remote?git branch -r
Why PR showing?git log main..origin/branch
Sync with GitHub?git fetch --prune
.