Last Updated: May 1, 2025
Branch Commands
git branchList all local branches (* marks current)
git branch -rList remote-tracking branches
git branch -aList all branches (local + remote)
git branch nameCreate a new branch at current HEAD
git checkout branchSwitch to an existing branch
git checkout -b nameCreate AND switch to a new branch
git switch branchSwitch branches (modern, safer alternative)
git switch -c nameCreate and switch (modern)
git branch -d nameDelete a merged branch (safe)
git branch -D nameForce delete an unmerged branch
Remote Branches
git push origin namePush a local branch to remote
git push -u origin namePush and set upstream tracking
git push origin --delete nameDelete a remote branch
git fetch --pruneClean up remote-tracking refs for deleted branches
git checkout --track origin/nameCreate local branch tracking remote
git branch -vvShow local branches with tracking info
git branch --mergedList branches already merged into current
git branch --no-mergedList branches not yet merged
git branch --contains commitFind branches containing a specific commit
git branch --sort=-committerdateList branches by most recent commit
Branching Strategies
| Strategy | Description | Best For |
|---|---|---|
| GitFlow | Separate main/develop/feature/release/hotfix branches | Large teams, scheduled releases |
| GitHub Flow | Feature branches merged to main via Pull Requests | Continuous deployment teams |
| GitLab Flow | Environment branches (staging, production) | Multi-environment projects |
| Trunk-Based | Short-lived branches, merge to main at least daily | CI/CD, small teams, monorepos |
| Feature Branch | One branch per feature, merge when complete | Most common pattern |
Pro Tip: Always create a new branch for each feature or fix. Never commit directly to main on shared projects.