Terminal GuideTerminal Guide

git branch Command Guide

List, create, or delete branches

6 min readLast updated: January 19, 2026
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

List

git branchList local branches
git branch -rList remote branches
git branch -aList all branches

Create

git branch <name>Create branch
git branch <name> <commit>Branch from commit
git checkout -b <name>Create and switch

Delete

git branch -d <name>Delete merged branch
git branch -D <name>Force delete
git push -d origin <name>Delete remote

Rename

git branch -m <new>Rename current
git branch -m <old> <new>Rename specific

Downloadable Image Preview

Failed to generate preview

Basic Usage

The git branch command manages branches in your repository. Branches are lightweight pointers to commits, making Git's branching model fast and efficient.

bash
# List all local branches
git branch

# Create a new branch
git branch feature-login

# Delete a branch
git branch -d feature-login

Listing Branches

bash
# List local branches (* indicates current)
git branch

# List remote branches
git branch -r

# List all branches (local and remote)
git branch -a

# List with more details (last commit)
git branch -v

# List branches merged into current branch
git branch --merged

# List branches not yet merged
git branch --no-merged

Creating Branches

bash
# Create a new branch (stay on current branch)
git branch feature-x

# Create branch from specific commit
git branch hotfix abc1234

# Create branch from another branch
git branch feature-y develop

# Create and switch in one command
git checkout -b feature-x
# Or using git switch (Git 2.23+)
git switch -c feature-x

# Create branch tracking a remote
git branch --track feature origin/feature

Deleting Branches

bash
# Delete a merged branch
git branch -d feature-done

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

# Delete remote branch
git push origin --delete feature-done

# Clean up stale remote-tracking branches
git remote prune origin
# Or during fetch
git fetch --prune
Warning
Using -D to force delete an unmerged branch will permanently lose any commits not merged elsewhere. Use with caution.

Renaming Branches

bash
# Rename current branch
git branch -m new-name

# Rename any branch
git branch -m old-name new-name

# Rename and update remote
git branch -m old-name new-name
git push origin --delete old-name
git push -u origin new-name

Practical Examples

bash
# Feature branch workflow
git checkout -b feature/user-auth
# ... make changes and commits ...
git checkout main
git merge feature/user-auth
git branch -d feature/user-auth

# Clean up merged branches
git branch --merged | grep -v "\*\|main\|develop" | xargs git branch -d

# See branch differences
git log main..feature-x

# Copy current branch
git branch backup-before-refactor

# Set upstream tracking
git branch -u origin/main

Frequently Asked Questions

What is the difference between git branch and git checkout -b?

"git branch <name>" creates a branch but stays on current branch. "git checkout -b <name>" creates and switches to the new branch in one command.

How do I delete a remote branch?

Use "git push origin --delete <branch-name>" or "git push origin :<branch-name>" to delete a branch from the remote repository.

What is the difference between -d and -D when deleting?

-d only deletes if the branch is fully merged. -D force deletes regardless of merge status. Use -D carefully as you may lose unmerged work.

How do I see which branches are merged?

Use "git branch --merged" to see branches merged into current branch, or "git branch --no-merged" to see unmerged branches.

How do I track a remote branch?

Use "git branch --track <branch> origin/<branch>" or "git checkout --track origin/<branch>" to create a local branch that tracks a remote.

Summary

git branch is essential for managing parallel development. Use branches liberally - they're cheap in Git.

Quick Reference

  • git branch - List branches
  • git branch <name> - Create branch
  • git branch -d <name> - Delete merged branch
  • git branch -m <new> - Rename current branch

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles