Git克隆仓库后仅显示单个分支,Android Studio无法识别其余分支
Hey there! Let's break down why you're only seeing a couple of branches and how to get all those exercise branches showing up properly.
What's Going On?
First off, deleting the wrong folder and re-cloning isn't the culprit here—this is just Git's default behavior. When you clone a repository, Git only pulls down the default branch (like develop_branch or StarterCode) by default. All other remote branches exist on the origin remote (your forked repo), but they don't automatically get local copies created for you. That's why git branch only shows those few branches, and trying to switch directly creates an empty local branch.
Step-by-Step Fixes
1. Pull All Remote Branch Information
First, fetch all the branch data from your forked remote repository:
git fetch origin
Now, if you run git branch -a, you'll see all branches—remote ones will be listed with the origin/ prefix (like origin/exercise-1, origin/exercise-2).
2. Create a Local Branch Linked to the Remote Exercise Branch
Instead of using git checkout [branch-name] (which creates an empty local branch), use this command to create a local branch that's connected to the remote one:
git checkout -b [exercise-branch-name] origin/[exercise-branch-name]
For example, if the exercise branch is exercise-jetpack-compose-basics, run:
git checkout -b exercise-jetpack-compose-basics origin/exercise-jetpack-compose-basics
If you already created an empty local branch by mistake, delete it first with:
git branch -D [mistaken-branch-name]
Then run the checkout command above.
3. Verify and Sync
After switching, run git pull to make sure your local branch is fully synced with the remote one. You should now see the exercise's code in Android Studio.
Android Studio GUI Alternative
If you prefer not to use the terminal, you can do this directly in Android Studio:
- Open the Git tool window (click the
Gittab at the bottom, or go to View > Tool Windows > Git) - Switch to the
Branchestab - Expand the
Remote Branchessection—you'll see all the exercise branches from your forked repo - Right-click the branch you want, select Checkout as new local branch
- Android Studio will handle creating the linked local branch and pulling the code automatically
Why This Didn't Happen With Other Projects?
Chances are, those other projects either had fewer branches (so you didn't notice), or you had already run git fetch on them before checking branches. This is standard Git behavior for new clones—nothing specific to this Udacity project, just how Git works with remote repositories.
内容的提问来源于stack exchange,提问作者George Ampartzidis

