4

First my terminology: "upstream" is the original apache repo (on github). "origin" is my fork of the apache repo (also on github).

After executing the following:

git remote update
git fetch

I see the apache repo references updated to include two new branches.

[cloudera@localhost delta]$ git remote update
Fetching origin
Fetching upstream
remote: Counting objects: 58, done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 58 (delta 6), reused 48 (delta 6)
Unpacking objects: 100% (58/58), done.
From https://github.com/apache/spark
   7e4a0e1..b060014  branch-0.9 -> upstream/branch-0.9
   1a0a2f8..952e0d6  branch-1.0 -> upstream/branch-1.0
 * [new branch]      branch-1.1 -> upstream/branch-1.1

Note the [new branch] created from the upstream. I did not have to do a "git branch -b". But what about the origin new branches (of which there are several)? Why the difference in behavior here?

None of the new branches on my local repo (created in a separate local clone) were fetch'ed/created in this clone.

So how to fetch the new branches in origin ?

UPDATE Based on suggestion by @VonC I did

git branch -avvv

The output DOES show the origin branches;

delta                            
* master                           
master
  remotes/origin/HEAD              
  remotes/origin/branch-0.5        
    ..
  remotes/origin/delta             
  remotes/origin/docs              st
  remotes/origin/strlen            
  remotes/upstream/branch-0.5      
   ..
  remotes/upstream/branch-1.1      
  remotes/upstream/master          
  ..

So my confusion then looks to be more of a basic/beginner one: why did

$ git branch
  delta
* master

Does not show for eampe remotes/origin/docs.. I guess I need to read up on the git branch command more here.

Another update @AD7Six has explained about further in his answer about the git branch vs git branch -r

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • What exactly do you mean by "none of the new branches on my local repo (created in a separate local clone) were fetch'ed/created in this clone"? What exactly were you expecting to see? – gravetii Aug 03 '14 at 14:46
  • I am trying to fetch branches created in the remote origin repository into the local clone. If "git remote update" were not the correct way, then please suggest one. – WestCoastProjects Aug 03 '14 at 14:46

3 Answers3

4

Fetching from the remotes only updates your remote references eg. branches like origin/master. Git does not automatically create local branches that you can use to update your remote branches.

To create a local branch from any of the remote branches, you need to do this -

git checkout -b newLocalBranch <remote_name>/<remote_branch_name>

Now, the branch newLocalBranch is said to be tracking the branch on your repository. So you can now work on your local newLocalBranch and use it to push your new commits to the remote branch using -

git push <remote_name> newLocalBranch:<remote_branch_name>
gravetii
  • 9,273
  • 9
  • 56
  • 75
  • As mentioned in the OP the "git remote update" automatically found new branches on the remote upstream (apache). I did not have to do "git checkout -b" to create them. So then why the difference in behavior between the upstream and the origin? – WestCoastProjects Aug 03 '14 at 14:51
  • 1
    @AD7six thanks, corrected terminology. @javadba You are not creating local branches when you fetch. You are just asking git to update your remote references. If you have to create a local branch, you SHOULD do a `git checkout -b`. So chances are you are just seeing the remote references being updated. – gravetii Aug 03 '14 at 14:53
  • @javadba Could you paste the contents of .git/config file in your question, at least the details pertaining to upstream and origin. – gravetii Aug 03 '14 at 14:59
  • The OP has been updated to show that the new branch 1.1 was created automatically from upstream - but none of the new branches from origin were created. – WestCoastProjects Aug 03 '14 at 15:00
  • @javadba, Dude, trust me! Git is NOT creating new branches for you! It's just updating the remote references. – gravetii Aug 03 '14 at 15:02
  • You can do a `git branch` if you want to see what all local branches you have, and I can almost surely say you won't have the 1.1 branch that you think git has created for you. – gravetii Aug 03 '14 at 15:04
  • @AD7six I have updated the OP. I have upvoted your answer but had seen the git -avvv from VonC first and awarded that one. I have a bit of a dizziness from git at this moment, thanks for the patience. – WestCoastProjects Aug 03 '14 at 15:13
  • I upvoted both yours and VonC because they were both helpful. I do not do that as a matter of course, but only when warranted. Thanks for your help. – WestCoastProjects Aug 03 '14 at 15:15
3

You need to fetch from ustream locally, then push those branches to your fork (origin).

git fetch upstream
git branch --set-upstream newBranch1 upstream/newBranch1 
git push origin newBranch1 

Your local repo (cloned of your fork) is the intermediate point in order to get new commits/branches from upstreams and publish them to origin.
There is no "direct replication" from one GitHub repo to another.

For fetching your own branches from origin, git fetch is enough, check with:

git branch -avvv

Then you can use a one-liner command to create local branches from the remote tracking ones that were fetched: see "git pull all branches from remote repository".


the "git remote update" automatically found new branches on the remote upstream (apache).
I did not have to do "git checkout -b" to create them.

So then why the difference in behavior between the upstream and the origin?

You should see the same git a git fetch (or git fetch origin): if they are in the result of a git branch -avvv, but were not fetched by your next git remote update, that means they were already present in your local clone.

In both cases, (upstream or origin), those branches are remotes tracking ones, as seen in git branch -r (r for remote).

You can check it by comparing the list of:

  • local branches: git branch
  • remote (tracking) branches: git branch -r

See more in this git fetch tutorial from Atlassian:

git fetch origin

This will display the branches that were downloaded:

a1e8fb5..45e66a4 master -> origin/master
a1e8fb5..9e8ab1c develop -> origin/develop
* [new branch] some-feature -> origin/some-feature

The commits from these new remote branches are shown as squares instead of circles in the diagram below.
As you can see, git fetch gives you access to the entire branch structure of another repository.

https://gp1.wac.edgecastcdn.net/8029C4/wac-small/wac/landing/git/tutorial/remote-repositories/pageSections/00/contentFullWidth/0/tabs/01/pageSections/02/contentFullWidth/00/imageBinary/git-tutorial-repos-fetch.png

The branches you see "created" are registered in the "remotes" namespace (in the OP case, the "remotes/upstream" one)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi VonC. Apparently my question were not clear enough. There are several branches on my own remote fork that I am trying to fetch into a different (local) clone. There is no "push"ing here. – WestCoastProjects Aug 03 '14 at 14:48
  • @javadba ok, I have added a link to create local brnaches from the remote you have fetched – VonC Aug 03 '14 at 14:51
  • @javadba I have edited the answer to address the difference of behavior. – VonC Aug 03 '14 at 14:56
  • I have updated the OP to show the output of the "git remote update": it clearly shows the creation of the new branch from upstream. – WestCoastProjects Aug 03 '14 at 14:59
  • @javadba that should be the creation of remote tracking branches only, not local branches. Check that with `git branch` vs. `git branch -r`. That is what I am saying in the last part of my answer. – VonC Aug 03 '14 at 15:02
  • OK so I did git branch -avvv It does show the local branch already exists. Then I did not realize that git branch does not show them. I have updated the OP. – WestCoastProjects Aug 03 '14 at 15:03
  • @javadba if `git branch` does not show them, it is because they are remote tracking branches, as I mentioned in the answer all along. – VonC Aug 03 '14 at 15:04
3

Git branch only shows local branches

Running git branch with no arguments, will show only local branches:

-> git branch
* develop
  master

To show only remote branches use the --remote (or -r) option:

-> git branch --remote
  origin/HEAD -> origin/master
  origin/develop
  origin/master

To show all branches use the --all (or -a) option:

-> git branch --all
* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/master

All commands can be combined with the verbose option to get more info:

-> git branch -vv
* develop 5cb42f9 [origin/develop: ahead 3] make logging configurable
  master  77de2a8 [origin/master: ahead 7] Merge branch 'develop'

For more info on the branch command arguments, see the documentation

Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123