SyntaxStudy
Sign Up
Git Creating and Switching Branches
Git Beginner 9 min read

Creating and Switching Branches

Branches let you diverge from the main line of development to work on features or fixes in isolation. The main (or master) branch is the default. Create a new branch for every feature or bug fix to keep your history clean.

Example
# List all local branches (* marks the current branch):
git branch

# List all branches including remote tracking branches:
git branch -a

# Create a new branch:
git branch feature/user-login

# Switch to an existing branch:
git switch feature/user-login

# Create and switch in one step (preferred):
git switch -c feature/user-login

# Old syntax (still valid):
# git checkout -b feature/user-login

# Rename a branch:
git branch -m old-name new-name

# Delete a merged branch:
git branch -d feature/user-login

# Force-delete an unmerged branch:
git branch -D feature/abandoned

# See the last commit on each branch:
git branch -v

# See which branches are merged into the current one:
git branch --merged

# HEAD — pointer to the current branch (or commit):
cat .git/HEAD
# ref: refs/heads/feature/user-login