1

I have mk branch checked out. Now I do git fetch origin to fetch origin/mk branch. This branch was rebased and I have no local changes on it so I simply want to move local mk branch pointer to the fetched remote one, so I do the following:

$ git update-ref -m "Updating mk to remote ..." refs/heads/mk refs/remotes/origin/mk

The operation seems to successful as proved by show-ref:

$ git show-ref mk
a885dad11c37e5c03cc4937766d62e181984c1aa refs/heads/mk
a885dad11c37e5c03cc4937766d62e181984c1aa refs/remotes/origin/mk

However, the commit a885dad wasn't checked out to working directory. Why? Can it be checkout automatically?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

1

Note, as mentioned in "Merge, update, and pull Git branches without using checkouts", a simple git fetch -u origin mk:mk would have done the same (for fast-foward merge).

The question is: are those commands updating HEAD or not?

In the case of the git update-ref, a separate git update-ref -m "Updating mk to remote ..." HEAD refs/heads/mk would be needed.

But using the -u in git fetch should allow to update HEAD as well:

-u
--update-head-ok

By default git fetch refuses to update the head which corresponds to the current branch. This flag disables the check.
This is purely for the internal use for git pull to communicate with git fetch, and unless you are implementing your own Porcelain you are not supposed to use it.

Note that those commands would not update the working tree.
You would still need a separate git checkout -- ..

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks! I don't quite understand why I would need to update `HEAD` in my case: since `mk` is checked out `HEAD` points to it already. I also tried to lookup this particular structure `git checkout -- .` and couldn't find - can you please point me where I can read about it (I assume it is similar to `reset --hard HEAD`)? – Max Koretskyi Feb 21 '15 at 13:12
  • I will, when I'll get back home in 2 hours – VonC Feb 21 '15 at 13:33
  • 1
    @Maximus it is similar indeed: http://stackoverflow.com/a/3639387/6309 I use git checkout usually because I have content filter driver (http://stackoverflow.com/a/27665008/6309, like https://github.com/VonC/compileEverything/blob/b7c1d6c48d76859605f4a06896e0e1f797656c82/gitolite/config#L13-L15), and they are triggered by checkout (https://github.com/VonC/compileEverything/blob/b7c1d6c48d76859605f4a06896e0e1f797656c82/gitolite/install_or_update_gitolite.sh#L21). Inb your case, a `git reset --hard` is enough. – VonC Feb 21 '15 at 15:40
  • got you, thanks a lot! Can you also comment please on this one _I don't quite understand why I would need to update HEAD in my case: since mk is checked out HEAD points to it already_? I assume that maybe you wasn't talking about my case in particular, but about `update-ref` in general in that it doesn't update HEAD, correct? – Max Koretskyi Feb 21 '15 at 16:17
  • @Maximus yes, correct. In your case, HEAD is already a symref to your branch (http://stackoverflow.com/a/28557592/6309) – VonC Feb 21 '15 at 16:21
  • fantastic, thanks a lot! Everything's clear now. Best! – Max Koretskyi Feb 21 '15 at 16:22