djechlin
djechlin

Reputation: 60758

Why does git fetch remote branch always give me master?

I have machine1 with git repo client, which has branches master and fixes_v3. I have machine2 and no matter what combination of things I try it pulls a branch and names it fixes_v3 but has the content of master. e.g.:

git clone git+ssh://user@machine1/home/user/client gives me this

git pull gives me this

git branch -a then checkout the remote fixes_v3 gives me this

git fetch gives me this

As well as various other combinations of these things, often starting over, aggregated from various SO questions, manuals, official documentation, etc. I don't even know where to begin researching this without having to learn git from scratch for the nth time.

How to get the code in fixes_v3 instead of just an improperly named branch fixes_v3?

Edit for requested output:

um@machine2:~/client$ git show-ref
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63 refs/heads/fixes_v3
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63 refs/heads/localfixesv3
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63 refs/remotes/origin/HEAD
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63 refs/remotes/origin/fixes_v3
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63 refs/remotes/origin/version3
um@machine2:~/unrollclient$ git ls-remote origin
um@machine1 password: 
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63    HEAD
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63    refs/heads/fixes_v3
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63    refs/heads/version3
7bd4490e8f98d29c5e82f473d1b04e542b67dec0    refs/remotes/origin/HEAD
16c7b55e2ea3e26c2f8faccd43e1c0db9620008a    refs/remotes/origin/Testing
a7a5642f6766332910c2c9005e8aafaf456f1f58    refs/remotes/origin/john
7bd4490e8f98d29c5e82f473d1b04e542b67dec0    refs/remotes/origin/master
ae10bcf7e15a4e251b50e8bc2eae3e5a2bc25b63    refs/remotes/origin/version3
um@machine2:~/client$ git remote show origin
um@machine1's password: 
* remote origin
  Fetch URL: git+ssh://um@machine1/home/um/client
  Push  URL: git+ssh://um@machine1/home/um/client
  HEAD branch (remote HEAD is ambiguous, may be one of the following):
    fixes_v3
    version3
  Remote branches:
    fixes_v3 tracked
    version3 tracked
  Local branches configured for 'git pull':
    fixes_v3     merges with remote fixes_v3
    localfixesv3 merges with remote fixes_v3
  Local ref configured for 'git push':
    fixes_v3 pushes to fixes_v3 (up to date)

Upvotes: 0

Views: 1855

Answers (2)

djechlin
djechlin

Reputation: 60758

Question turned out to be even dumber than I thought. Changes in fixes_v3 branch were not committed... after re-reading the git pro guide I was able to understand git well enough to troubleshoot such a basic step. Thanks everyone.

Upvotes: 0

Dolanor
Dolanor

Reputation: 820

get the remote branch fixes_v3 from origin repo

git fetch origin fixes_v3

Create a branch

git checkout -b myLocalFixes_v3 origin/fixes_v3

Should do the trick

Explanations of what you are doing

git clone git+ssh://user@machine1/home/user/client

You're cloning the repo locally, by default, your local repo will be checked-out on the HEAD revision of the remote repo. Which by default is master

git pull 

Git pull (without options) will fetch then merge the default tracking branch onto your current branch. As you were locally on master, which was originated/tracked the remote master branch, then it will fetch (download in the cache) the remote master branch, then merge it on your current branch which is master.

git branch -a then checkout the remote fixes_v3 gives me this

The checkout is the tricky part. The checkout I gave you should work. I did wrong checkout in the past too because I didn't get all the subtleties. If you do

git checkout -b origin/fixes_v3

It will create a local branch called "origin/fixes_v3" which is based on master

git checkout -b origin/fixes_v3 master

Same here

git checkout -b origin/fixes_v3 origin/fixes_v3

It will create a local branch called "origin/fixes_v3" which is based on origin/fixes_v3 the remote "fixes_v3". You shouldn't do it though, because git will have problems figuring out after which "origin/fixes_v3" it is : the remote branch, or the local branch named with a "/" in it ?

git fetch

Git fetch only downloads the remote branches/commit in your local cache. It will do nothing else, no merge, no checkout, no rebase.

If your remote name is : origin, it will download the cache in the .git/refs/remotes/origin/ directory (I simplify) So you will have .git/refs/remotes/origin/master and .git/refs/remotes/origin/fixes_v3. When you will do a git checkout -b origin/fixes_v3 origin/fixes_v3 It will create a .git/refs/head/origin/fixes_v3 based on .git/refs/remotes/origin/fixes_v3, but then you will have conflict in names because won't figure if the origin/fixes_v3 means the remote on the origin remote, or the local branch named origin/fixes_v3

Upvotes: 1

Related Questions