Max Koretskyi
Max Koretskyi

Reputation: 105499

how to automatically update working tree when updating-ref of a checked out branch

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?

Upvotes: 1

Views: 620

Answers (1)

VonC
VonC

Reputation: 1324148

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

Upvotes: 1

Related Questions