Skip to main content

Git Branch Cheatsheet — Create, Merge & Manage Branches

Branching is the most powerful feature of Git. This guide covers everything from creating your first branch to handling complex merges and rebases — the foundation of any collaborative workflow.

Updated: 2026-07-15·17 commands

Quick Reference

ScenarioCommand
List local branchesgit branch
List all branches (including remote)git branch -a
Create and switch to a new branchgit checkout -b feature/login
Create (no switch)git branch feature/login
Switch to existing branchgit checkout main or git switch main
Rename a branchgit branch -m old-name new-name
Delete a merged branchgit branch -d feature/done
Force delete unmerged branchgit branch -D feature/abandoned
Merge a branch into currentgit merge feature/login
Rebase current branch onto anothergit rebase main

Core Commands

### Creating & Switching Branches

``bash # Create a new branch (stays on current branch) git branch feature/new-widget

# Create and switch immediately git checkout -b feature/new-widget

# Same with the newer git switch command git switch -c feature/new-widget

# Switch to an existing branch git checkout main git switch main # newer alternative `

### Listing Branches

`bash # Local branches only git branch

# All branches (including remote tracking) git branch -a

# Show last commit on each branch git branch -v

# Show merged branches (safe to delete) git branch --merged `

### Merging

`bash # Standard merge (creates merge commit) git checkout main git merge feature/login

# Fast-forward only (abort if not possible) git merge --ff-only feature/login

# Squash merge (flatten all commits into one) git merge --squash feature/login `

### Rebasing

`bash # Rebase current branch onto main git checkout feature/login git rebase main

# Interactive rebase (squash, reorder, edit commits) git rebase -i HEAD~3

# Abort a rebase that went wrong git rebase --abort `

### Deleting Branches

`bash # Delete a fully merged branch git branch -d feature/complete

# Force delete (even if not merged) git branch -D feature/abandoned

# Delete remote branch git push origin --delete feature/complete ``

Branch(17)

CommandLevel
git branch
List, create, or delete branches
Basic
git branch -d
Delete a merged branch
Basic
git branch -D
Force delete a branch regardless of merge status
Basic
git branch -a
List all local and remote branches
Basic
git branch -r
List remote branches
Basic
git branch --merged
List branches already merged into the current branch
Basic
git branch --no-merged
List branches not yet merged into the current branch
Basic
git branch --contains
List branches that contain a specific commit
Intermediate
git branch --show-current
Show the current branch name
Basic
git branch -m
Rename a branch
Basic
git branch --move
Rename a branch (long option form)
Basic
git branch -vv
Show branch tracking relationships
Intermediate
git branch --edit-description
Edit the description of a branch
Intermediate
git switch
Switch branches
Basic
git checkout -b
Create and switch to a new branch
Basic
git merge
Join two or more development histories together
Intermediate
git rebase
Reapply commits on top of another base tip
Expert

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-15.