In Git, working with branches is essential for managing features, bug fixes, and collaboration. One of the most common tasks is switching back to the main development branch—often called master
or main
. This guide walks you through how to move to the master
branch safely and efficiently.
🧭 What Is the master
Branch?
The master
branch (or sometimes main
) is typically the default branch in a Git repository. It’s often used to store production-ready or stable code.
💡 Note: Many modern GitHub repositories now use
main
instead ofmaster
as the default branch name. Always double-check which one your repo uses.
✅ Step-by-Step: Move to the master
Branch
🔄 1. Check Existing Branches
Run the following to see all local branches:
git branch
And to see remote branches:
git branch -r
This helps confirm whether master
exists.
🔁 2. Switch to the master
Branch
If the master
branch exists locally:
git checkout master
If you’re using Git 2.23+ (recommended), you can also use:
git switch master
🌐 3. If master
Doesn’t Exist Locally
You can create a local copy of the remote master
branch:
git checkout -b master origin/master
Or with the newer switch command:
git switch -c master origin/master
This creates a local branch that tracks the remote master
.
🔄 4. Pull the Latest Changes
Once you’re on master
, update it with the latest code:
git pull origin master
🧠 Bonus: Check Your Current Branch
Use this at any time to confirm which branch you’re on:
git branch
Your current branch will have an asterisk (*
) next to it.
🔚 Summary
Task | Command |
---|---|
List all local branches | git branch |
List all remote branches | git branch -r |
Switch to master (Git < 2.23) | git checkout master |
Switch to master (Git ≥ 2.23) | git switch master |
Pull latest master updates | git pull origin master |
⚠️ Final Tips
- Always commit or stash your changes before switching branches to avoid conflicts.
- Confirm whether your repo uses
master
ormain
by checkinggit branch -a
.