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?
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.
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?
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