Fixing “Landed on Main”: How to Access Remote Git Branches After Cloning
Cloning a repository is exciting — new code, new adventure.
But sometimes Git drops you straight onto main when you really wanted that shiny dev branch.
Remote (origin) Local
------------------ ------------
origin/main main ← default after clone
origin/dev ----> dev ← your new branch
git clone is a Git command that downloads a repository’s full history but only fully maps and checks out the default branch, usually main or master; other remote branches exist in the history but are not yet registered locally. No worries. Here’s the quick rescue plan.
git fetch origin: Registering Remote Branches Locally
First, tell Git to look for other branches on the remote:
git fetch origin
git fetch origin is a Git command that downloads remote branch pointers and commits from the origin remote without modifying your current working directory files. 👉 git fetch origin is the magic unlock: it updates your local repository with all branches that exist on the remote (like dev, feature-x, etc.). Without it, your local machine doesn’t even know those remote branches exist.
| Git Concept | Architectural Explanation |
|---|---|
| Cloning Limitations | Cloning downloads the repository history but only fully maps and checks out the default branch (usually main or master). |
The Role of fetch |
git fetch origin securely downloads remote branch pointers and commits without modifying your current working directory files. |
| The Result | Once fetched, your local Git index is aware of origin/dev, allowing you to switch to it seamlessly. |
Error Fix: “error: pathspec ‘dev’ did not match any file(s) known to git”
This error appears when you try git checkout dev (or git switch dev) before Git knows the remote branch exists. The fix is two commands: run git fetch origin first to register origin/dev locally, then git switch dev (or git checkout dev) to create the tracking branch and move onto it.
Git Branch Navigation Commands: switch, pull, stash Cheat Sheet
Now that Git is aware of the remote branches, you can freely navigate. Here are the core commands you need:
| Command | Action | Explanation |
|---|---|---|
git switch dev |
Switch Branch | Moves your working directory to the dev branch. (Note: The older git checkout -b dev origin/dev works identically but is less intuitive). |
git pull |
Synchronise | Pulls down the latest updates from the remote to ensure your local dev branch is completely synchronised. |
git stash |
Save Work | If you have uncommitted changes on main that you don’t want to lose, this temporarily shelves them. |
git stash pop |
Restore Work | Once you have switched to the correct branch, this command reinstates your shelved changes. |
Done. No tears, no confusion — just smooth Git moves. 🚀
Related tools you may want to try next.
B12.io Recently, I have found an AI-powered platform that enables you to create professional websites, pages, posts, and emails with ease. I will also give it a try and soon write a new post about B12.io (I am working on my coding post at the moment :).
Summary: Git Fetch and Switch Workflow for Remote Branches
That’s it — a simple way to jump from main to where the real work lives.
Remember: git fetch origin is your secret handshake to see all remote branches.
Next time you clone and panic, you’ll know exactly what to do :)
Git Fetch and Branch Switching FAQ
Why does git clone only give me the main branch?
Cloning downloads the full repository history but only fully maps and checks out the default branch, usually main or master. Remote branches like dev exist in the repository’s history but are not registered locally until you run git fetch.
What does git fetch origin actually do?
git fetch origin downloads remote branch pointers and commits from the origin remote without modifying your working directory files. It makes your local Git installation aware of branches like origin/dev so you can switch to them.
What is the difference between git switch and git checkout -b for remote branches?
git switch dev and the older git checkout -b dev origin/dev both create a local tracking branch from the matching remote branch and move your working directory to it. git switch is the newer, more explicit command, introduced to separate branch switching from file checkout.
How do I keep uncommitted changes when switching branches?
Run git stash to temporarily shelve uncommitted changes before switching branches, then run git stash pop after switching to restore them.
I get ‘error: pathspec did not match any file(s) known to git’ when checking out a branch. How do I fix it?
This happens when you try to check out a remote branch before Git knows it exists locally. Run git fetch origin first, then git switch <branch> (or git checkout <branch>).
References
- Git Basics - Working with Remotes — git-scm.com
- git-switch Documentation — git-scm.com
- git-stash Documentation — git-scm.com
Did you like this post? Please let me know if you have any comments or suggestions.
Git posts that might be interesting for youEnjoyed this? Get more like it.
Weekly notes on AI tools, Python, and what I'm actually building — plus a free copy of Fantastic AI: The 2026 Toolkit.