1

I have a sample Android project in Udacity, consisting of several exercises, each one of them in a different branch. I have forked the repository and then git cloned the forked version from the Android Studio terminal. However, when I git branch to see the available branches, the only branch existing is the one named develop_branch or StarterCode. You can see the repo here. So, If I try to do git branch to some of the exercises, a new branch is created. On top of that, the code I see in Android Studio is from the only branch that was cloned... I have tried to implement some solutions to similar problems like Git- My branches are not showing after cloning a repo or Only master branch is visible after cloning a Git repo, however I think that specific problem is different. I believe that it has to do with Android Studio specifically.

I'd like to add that I had done the same thing with a different project a couple of days ago, without those problems. Everything worked fine. The only difference is that when I first cloned the repo, I had cloned in a wrong folder. Then I deleted it and cloned it to the folder I wanted. If that's what's causing the problem, is there a way to fix it?

  • Do `git branch -a` to see all remote branches available. – Tim Biegeleisen May 12 '18 at 10:47
  • @castis when I git fetch --all, it says "Fetching origin". After that, if I check again, I still don't see the branches. – George Ampartzidis May 12 '18 at 10:50
  • @TimBiegeleisen git branch -a returns a list of all the branches, beginning with remotes/origin. So, al of them are like remotes/origin/T0X.01-Exercise-GoogleApiClient and so on. – George Ampartzidis May 12 '18 at 10:53
  • And do you see any names which look like the branches you want? – Tim Biegeleisen May 12 '18 at 11:02
  • First, read a short git tutorial to understand the basics. Your problem is that you have the remotes but have to checkout them one by one to create your corresponding local tracking branches. (And you don't need 'fetch - -all' that fetch from all **remotes**. A normal fetch already get all the branches of the origin remote) – Philippe May 12 '18 at 11:04
  • @TimBiegeleisen, yes all the branch names are the ones I want. The only difference is that each one starts with "remotes/origin" – George Ampartzidis May 12 '18 at 11:07
  • @Philippe, I understand that I have to checkout each branch one by one, but how will I do this? And my big question is why this happened in this case while it didn't happen previously? – George Ampartzidis May 12 '18 at 11:10
  • Possible duplicate of [How to clone all remote branches in Git?](https://stackoverflow.com/questions/67699/how-to-clone-all-remote-branches-in-git) – phd May 12 '18 at 12:28

3 Answers3

1

I have forked the repository and then git cloned the forked version from the Android Studio terminal. However, when I git branch to see the available branches, the only branch existing is the one named develop_branch or StarterCode.

It's the way git works. When you clone a git repo with multiple branches locally, only the default branch will be checkout. Based on the remote repo, the default branch is develop_branch for now. So when you clone git repo from github, the only local branch is develop_branch.

To switch to another branch correspond to remote branches, you can use the command:

git checkout <remote branch name>

Then the remote branch will be checkout locally.

To check the difference between two branches, you can use the command:

git diff branchname1 branchname2 --name-only

Such as you can view the difference between develop_branch and T0X.01-Exercise-GoogleApiClient by git diff develop_branch T0X.01-Exercise-GoogleApiClient --name-only.

If the file has different version between develop_branch and T0X.01-Exercise-GoogleApiClient, but it shows same code in android studio, then you can troubleshot with below aspects:

  1. Check if the branch has been switched successful

    Assume you need to switch from develop_branch to T0X.01-Exercise-GoogleApiClient, you can check the current branch in the bottom of the android studio window.

    enter image description here

  2. Close the file and reopen

    If the file content does not change correspondingly to different branch, you can close the file and reopen, then check the content again.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
0

Running git branch -a apparently does reveal that you have tracking branches for the branches you want, and you said in a comment:

yes all the branch names are the ones I want

Then all you need to do to checkout a local branch is something like

git checkout T0X.01-Exercise-GoogleApiClient

Git will automatically create a local branch which tracks origin/T0X.01-Exercise-GoogleApiClient.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Doing so results in the following: `Branch 'T0X.01-Exercise-GoogleApiClient' set up to track remote branch 'T0X.01-Exercise-GoogleApiClient' from 'origin'. Switched to a new branch 'T0X.01-Exercise-GoogleApiClient'` . However, the code showing up in Android Studio isn't the code corresponding to the T0X.01 Exercise- it stays just the same to the StarterCode. – George Ampartzidis May 12 '18 at 11:14
  • Is it possible that perhaps this branch does not have the code you think it does? – Tim Biegeleisen May 12 '18 at 12:15
  • I am perfectly sure that all branches contain the code I am looking for. I can see it in my forked repo in Github – George Ampartzidis May 12 '18 at 15:31
0

I don't know if it's considered a solution to my problem, however I would like to mark the answer as solved. What I finally figured out is that somehow the only thing that was cloned locally was only one branch, so whenever I tried to switch to another branch, it was only being created without actually tracking a corresponding branch. After I deleted the project from my machine and did a git clone for the third time, it was finally cloned correctly.

Funny thing is that I was always copying the same web url of the repo. I cannot understand what caused this behaviour.