Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git fetch remote branch and remote ref

Tags:

git

how do I fetch a remote branch and update git's local ref for that branch without effecting the current branch? For example if I do this

$ git pull origin master

origin/master is merged into my current branch. This also doesn't work

$ git fetch origin master

As then I check

$ git branch -r -v
origin/HEAD   -> origin/master
origin/master 7cf6ec5 test 02

that origin/master ref "7cf6ec5 test 02" is behind. The real origin/master is a "XXXXXX test 03". git fetch only pulled the changes into FETCH_HEAD it didn't upload the local origin/master ref. What's the step for updating that ref?

Note:

$ git fetch origin

Will get all remote refs and update them but unfortunately that's (a) a lot of clutter. (I don't want someone else's 30-50 random branches knowing that someone wouldn't want mine) and (b) as branches are deleted on origin those refs don't get deleted locally the next time I do git fetch origin which means that path ends up with cruft.

The question is, how do fetch just one branch and update its ref locally?

like image 779
gman Avatar asked Dec 18 '12 07:12

gman


2 Answers

You can always provide a refspec to the git fetch command. In particular, if you want to update other-branch, you can do:

git fetch origin other-branch:other-branch

That will fetch the current tip of refs/heads/other-branch from origin, and put it into a local branch of the same name.

Another way, is to go ahead and to:

git fetch origin other-branch

And then recreate the local branch:

git branch -l -f other-branch -t origin/other-branch

The -l is there as a safety mechanism. It'll create a reflog entry, so that you can get back to the previous version if you need to.

like image 111
John Szakmeister Avatar answered Sep 24 '22 10:09

John Szakmeister


I think what I'm looking for is this

$ git fetch origin master:refs/remotes/origin/master

That does seem to work in that unlike 'git fetch origin master' I do actually see the ref get updated.

Any reason that's the wrong answer? Something I should be scared of?

like image 30
gman Avatar answered Sep 22 '22 10:09

gman