When working with Git, branches are essential for managing parallel versions of a project. Whether you’re developing a new feature, fixing a bug, or testing an idea, branches help you stay organized and collaborative. Knowing how to switch between them efficiently is a fundamental skill for every developer.
In this blog post, we’ll walk through how to change branches in Git using the command line—step by step.
📘 What Is a Git Branch?
A branch in Git represents an independent line of development. The default branch in most repositories is called main
or master
, but you can create as many branches as you need.
Common use cases for branches include:
- Developing new features
- Fixing bugs
- Experimenting with ideas
- Collaborating on shared workstreams
🔁 How to Change Branches in Git
Step 1: Open Your Command Line Tool
You can use Terminal on macOS/Linux or Command Prompt / Git Bash / PowerShell on Windows.
Navigate to your local Git repository:
cd path/to/your/project
Step 2: View Existing Branches
Before switching, you might want to see which branches are available:
git branch
This will list all local branches. The currently active branch will be highlighted with an asterisk *
.
To see remote branches, use:
git branch -r
Or to see all branches (local + remote):
git branch -a
Step 3: Switch to an Existing Branch
To change to another branch:
git checkout branch-name
Or using the newer Git command (since Git 2.23+):
git switch branch-name
Example:
git checkout develop
# or
git switch develop
✅ Tip: If you’re not sure what branch you’re currently on, run
git status
.
Step 4: Create and Switch to a New Branch
If the branch doesn’t exist yet and you want to create and switch to it in one command:
git checkout -b new-branch-name
# or
git switch -c new-branch-name
Example:
git checkout -b feature/user-login
This creates a new branch called feature/user-login
and switches to it.
⚠️ Common Errors and Fixes
1. “error: pathspec ‘branch-name’ did not match any file(s) known to git”
You may have mistyped the branch name or the branch doesn’t exist locally.
To fix:
- Run
git branch -a
to verify the branch exists. - If it’s a remote branch, check it out like this:
git checkout -b branch-name origin/branch-name
🧼 Bonus: Clean Up Local Branches
Over time, local branches can pile up. To delete a local branch:
git branch -d branch-name
Use -D
to force delete:
git branch -D branch-name
📝 Summary
Task | Command |
---|---|
List local branches | git branch |
List all (local + remote) | git branch -a |
Switch to a branch | git checkout branch-name or git switch branch-name |
Create and switch to new branch | git checkout -b branch-name or git switch -c branch-name |
Delete a local branch | git branch -d branch-name |
🚀 Final Thoughts
Changing branches in Git via the command line is quick and powerful. Once you master it, you’ll find working with multiple features, hotfixes, or releases much more efficient. Start practicing with a small repository, and you’ll be branching like a pro in no time.