When working with Git, branches are essential for managing features, bug fixes, and collaborative development. If you’re using Git Bash (the command-line interface for Git on Windows), changing branches is a simple process—but it’s important to understand the commands and what they do.
In this blog, you’ll learn how to list, switch, and manage branches in Git Bash like a pro.
🧠 What Is a Git Branch?
A branch in Git is a pointer to a specific snapshot of your project history. It lets you work on different features or experiments without affecting the main codebase.
Common branches:
main
ormaster
: the default, production-ready branchfeature/your-feature
: a development branchbugfix/issue-123
: a branch for resolving a bug
🛠 How to Change Branch in Git Bash
1. Open Git Bash
First, open Git Bash and navigate to your project directory:
cd path/to/your/project
2. List Available Branches
To see all branches in your repository:
git branch
Output might look like:
* main
feature/login-form
bugfix/typo
The asterisk
*
indicates your current branch.
3. Switch to Another Branch
To change (checkout) to a different branch:
git checkout branch-name
Example:
git checkout feature/login-form
This command switches your working directory to that branch.
4. (Git 2.23+) Use git switch
(Recommended)
If you’re using Git version 2.23 or later, you can also use the more intuitive git switch
:
git switch branch-name
Example:
git switch bugfix/typo
✅
git switch
is safer and easier to use, especially for new users.
5. Create and Switch in One Command
If the branch doesn’t exist yet, you can create and switch to it:
git checkout -b new-branch-name
Or with the newer command:
git switch -c new-branch-name
🚦 Bonus: Check Current Branch
At any time, you can check your current branch with:
git branch
or view detailed status with:
git status
🧠 Summary Table
Action | Command |
---|---|
List branches | git branch |
Switch branch | git checkout branch-name or git switch branch-name |
Create and switch | git checkout -b new-name or git switch -c new-name |
Check current branch | git status or git branch |
🚀 Final Thoughts
Switching branches in Git Bash is fundamental to working effectively with Git. Whether you’re building a feature, fixing a bug, or testing changes, mastering git checkout
and git switch
helps you work faster and safer.
Keep branching, stay organized—and remember to always commit your changes before switching branches!