Git Branching Cheat Sheet

Master Git branching — create, switch, merge, and delete branches. Complete branch workflow reference for teams.

Last Updated: May 1, 2025

Branch Commands

git branch
List all local branches (* marks current)
git branch -r
List remote-tracking branches
git branch -a
List all branches (local + remote)
git branch name
Create a new branch at current HEAD
git checkout branch
Switch to an existing branch
git checkout -b name
Create AND switch to a new branch
git switch branch
Switch branches (modern, safer alternative)
git switch -c name
Create and switch (modern)
git branch -d name
Delete a merged branch (safe)
git branch -D name
Force delete an unmerged branch

Remote Branches

git push origin name
Push a local branch to remote
git push -u origin name
Push and set upstream tracking
git push origin --delete name
Delete a remote branch
git fetch --prune
Clean up remote-tracking refs for deleted branches
git checkout --track origin/name
Create local branch tracking remote
git branch -vv
Show local branches with tracking info
git branch --merged
List branches already merged into current
git branch --no-merged
List branches not yet merged
git branch --contains commit
Find branches containing a specific commit
git branch --sort=-committerdate
List branches by most recent commit

Branching Strategies

StrategyDescriptionBest For
GitFlowSeparate main/develop/feature/release/hotfix branchesLarge teams, scheduled releases
GitHub FlowFeature branches merged to main via Pull RequestsContinuous deployment teams
GitLab FlowEnvironment branches (staging, production)Multi-environment projects
Trunk-BasedShort-lived branches, merge to main at least dailyCI/CD, small teams, monorepos
Feature BranchOne branch per feature, merge when completeMost common pattern
Pro Tip: Always create a new branch for each feature or fix. Never commit directly to main on shared projects.