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-headerThe * 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/developList with Details
bash
1git branch -v
2
3# Output:
4* main abc1234 Latest commit message
5 feature-login def5678 Working on loginCreating 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 branchMethod 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-newMethod 3: git switch -c (Modern)
bash
1# Create AND switch (newer syntax)
2git switch -c feature-newSwitching Branches
Using git checkout
bash
1# Switch to an existing branch
2git checkout main
3
4# Switch to another branch
5git checkout feature-loginUsing git switch (Modern)
bash
1# Switch to an existing branch
2git switch main
3
4# Switch to another branch
5git switch feature-logingit checkout vs git switch
| Command | Purpose |
|---|---|
git checkout | Multi-purpose (branches, files, commits) |
git switch | Only 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 abc1234Create 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 developWorking 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 hereTrack 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-remoteSet Upstream for Existing Branch
bash
1git branch --set-upstream-to=origin/feature-remoteQuick Reference
| Task | Command |
|---|---|
| List branches | git branch |
| Create branch | git branch name |
| Switch branch | git checkout name or git switch name |
| Create + switch | git checkout -b name or git switch -c name |
| Delete branch | git 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-newSummary
Key commands for branches:
- View:
git branch - Create:
git branch nameorgit checkout -b name - Switch:
git checkout nameorgit switch name - Create + Switch:
git checkout -b nameorgit switch -c name
Practice these commands in the next exercises!
