joey
joey

Reputation: 49

Git clone branches

I am facing a problem with GitHub: We have an assignment, and we should work on 3 branches, and after everybody finishes their work we need to merge the branches. We created a repo on GitHub (assuming I created that) and I invited my two fellows to have access.

When I created the repo in the terminal I push the empty files to GitHub (we didn't start working yet). Now we started to create the branches, me as the master I run in the terminal those three commands:

git branch one
git branch two
git branch three

So then when I run git branch I should see the four branches: one, two, three, master. I push the code to GitHub and all is fine.

The problem started when my friends and I want to clone the repo to our machines, for example my first colleague runs git clone reponame and when he runs git branch he just sees the branch master.

My questions:

  1. Why did this happen, why we don't see the branches with the clone?
  2. How can we clone the repo with all the branches included?

Upvotes: 2

Views: 92

Answers (1)

torek
torek

Reputation: 490068

So then when I run git branch I should see the four branches one - two - three -master ...

Yes. You created three new branches in your local repository, so the three branches exist. Your Git created master initially, so that exists as well.

I push the code to GitHub and all is fine.

When you run git push, you are pushing commits to GitHub. After any new commits that you have, that they don't, that they will need, arrive at GitHub, your Git asks GitHub's Git to create or update some branch name(s) in their repository.

If you run:

git push github one

the commits you send to GitHub will be those found via your branch name one. (If they already have some or all of those commits, your Git gets to skip sending them, making the push faster.) Then your Git asks their Git to create or update their branch name one.

If you run:

git push github one two three

you'll have your Git send commits needed for all three branches, if necessary,and then politely ask GitHub to create or update all three branch names.

You didn't say precisely what kind of git push you ran, so we can't be sure what names you created, if any, over on GitHub.

The problem started when my friends and I want to clone the repo to our machines, example my first colleague runs git clone reponame and when he runs git branch he just sees the master.

A git clone command is shorthand for several Git commands all in a row (plus one non-Git command first, if/as needed, to make an empty directory in which to run the Git commands). These commands:

  • copy all the commits from the other Git repository (over on GitHub), but
  • copy none of the branches.

Instead, your own Git will take their branch names—which could be just master or main, or could be all four branch names, as we already noted—and change those names into non-branch names. The names your Git produces are:

  • origin/master (or origin/main) for their master (or main);
  • origin/one for their one, if they have it;
  • origin/two for their two, if they have it;

and so on. These are remote-tracking names, not branch names. They will be printed out by git branch -r and git branch -a, but not by git branch.

Having made these remote-tracking names from their branch names, your Git now creates one branch name in your clone. This is the last step of git clone: it takes the branch name you said to use, with your -b option, and makes that name—using the origin/ version of the same name to find the right commit—and then checks out that commit, so that you can see the files that are in that commit.

If you don't use a -b option, your Git will ask the other Git what name it recommends. The standard recommendation is master or main (whichever of those they have). So that's why you all got a master.

Upvotes: 3

Related Questions