Git Clone and Branch Switching Explained
In the previous blog, we learned about cloning specific branches and shallow clones.
Now let’s go a step further and see what happens after a normal git clone, how Git sets up the default branch, and how you can switch or rename branches locally.
Default Branch After git clone
When you run a normal clone:
git clone <repo-url>
cd <repo-folder>
By default:
- Git checks out the default branch of the repository (often
mainormaster). - All remote branches are downloaded as remote tracking branches.
Check available branches:
git branch
👉 Output:
* main
Only the default branch is checked out locally.
Now check all branches:
git branch -a
👉 Output:
* main
remotes/origin/main
remotes/origin/test
remotes/origin/audiocall
✅ Git cloned all remote branch pointers, but only the default branch is active locally.
Checking Out Another Branch
Suppose you want to switch to the test branch:
git switch test
👉 What happens?
- Git creates a local branch
test. - It automatically sets the upstream branch to
origin/test.
Verify with:
git branch -vv
👉 Output:
* test a1b2c3d [origin/test] Commit message here
main f9e8d7c [origin/main] Commit message here
✅ The local test branch now tracks origin/test.
Creating a Local Branch with a Different Name
Now here’s something interesting:
Suppose the remote repository has a branch called audiocall, but you want your local branch to be called feature1.
You can do this:
git switch -c feature1 origin/audiocall
👉 This creates:
- A local branch
feature1. - That tracks the remote branch
origin/audiocall.
Check with:
git branch -vv
👉 Output:
* feature1 d4e5f6g [origin/audiocall] Commit message here
main f9e8d7c [origin/main] Commit message here
test a1b2c3d [origin/test] Commit message here
✅ Local name can be different, while still pointing to the same remote branch.
Visual Representation
mainmaps toorigin/maintestmaps toorigin/testfeature1maps toorigin/audiocall
Summary
- After cloning, Git checks out the default branch locally.
- All other branches exist as remote tracking branches.
- Use
git switch <branch>to move to another remote branch. - Use
git switch -c <new-local-name> origin/<remote-branch>to give a remote branch a different local name.
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉