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
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>
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:
git branch
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.
The branches you see "created" are registered in the "remotes
" namespace (in the OP case, the "remotes/upstream
" one)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With