Skip
Arish's avatar

14. Create and Switch Branches


Creating and Switching Branches

Let's learn the essential commands for working with branches.

Viewing Branches

List Local Branches

bash
1git branch
2
3# Output:
4* main
5  feature-login
6  bugfix-header

The * indicates your current branch.

List All Branches (Including Remote)

bash
1git branch -a
2
3# Output:
4* main
5  feature-login
6  remotes/origin/main
7  remotes/origin/develop

List with Details

bash
1git branch -v
2
3# Output:
4* main          abc1234 Latest commit message
5  feature-login def5678 Working on login

Creating Branches

Method 1: git branch

bash
1# Create a new branch (but don't switch to it)
2git branch feature-new
3
4# Verify it was created
5git branch

Method 2: git checkout -b

bash
1# Create AND switch to new branch
2git checkout -b feature-new
3
4# This is equivalent to:
5git branch feature-new
6git checkout feature-new

Method 3: git switch -c (Modern)

bash
1# Create AND switch (newer syntax)
2git switch -c feature-new

Switching Branches

Using git checkout

bash
1# Switch to an existing branch
2git checkout main
3
4# Switch to another branch
5git checkout feature-login

Using git switch (Modern)

bash
1# Switch to an existing branch
2git switch main
3
4# Switch to another branch
5git switch feature-login

git checkout vs git switch

CommandPurpose
git checkoutMulti-purpose (branches, files, commits)
git switchOnly for switching branches

git switch was introduced in Git 2.23 to make branch switching clearer.

Create Branch from Specific Commit

bash
1# Create branch from a specific commit
2git branch feature-old abc1234
3
4# Create and switch from specific commit
5git checkout -b feature-old abc1234

Create Branch from Another Branch

bash
1# First, switch to the base branch
2git checkout develop
3
4# Then create your feature branch
5git checkout -b feature-new
6
7# Or do it in one command
8git checkout -b feature-new develop

Working with Branches Example

bash
1# Start on main
2git checkout main
3
4# Create and switch to feature branch
5git checkout -b feature-user-profile
6
7# Make changes
8echo "Profile page" > profile.html
9git add profile.html
10git commit -m "Add profile page"
11
12# Switch back to main
13git checkout main
14
15# Your changes are in feature branch, not main
16ls  # profile.html is not here
17
18# Switch back to feature
19git checkout feature-user-profile
20ls  # profile.html is here

Track Remote Branches

Create Local Branch from Remote

bash
1# Fetch remote branches
2git fetch origin
3
4# Create local branch tracking remote
5git checkout -b feature-remote origin/feature-remote
6
7# Or using the shorthand (if branch name is unique)
8git checkout feature-remote

Set Upstream for Existing Branch

bash
1git branch --set-upstream-to=origin/feature-remote

Quick Reference

TaskCommand
List branchesgit branch
Create branchgit branch name
Switch branchgit checkout name or git switch name
Create + switchgit checkout -b name or git switch -c name
Delete branchgit branch -d name

Common Issues

Uncommitted Changes

If you have uncommitted changes:

bash
1# Option 1: Commit them
2git add .
3git commit -m "Save work"
4git checkout other-branch
5
6# Option 2: Stash them
7git stash
8git checkout other-branch
9git stash pop  # Restore changes
10
11# Option 3: Force switch (dangerous!)
12git checkout -f other-branch  # Loses changes!

Branch Already Exists

bash
1git checkout -b feature-new
2# fatal: A branch named 'feature-new' already exists.
3
4# Solution: Just switch to it
5git checkout feature-new

Summary

Key commands for branches:

  • View: git branch
  • Create: git branch name or git checkout -b name
  • Switch: git checkout name or git switch name
  • Create + Switch: git checkout -b name or git switch -c name

Practice these commands in the next exercises!