如何在更新签出分支的引用时自动更新工作树

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

我已经检查了 mk 个分支。现在我做 git fetch origin 来获取 origin/mk 分支。这个分支被重新设置了基础,我没有对它进行本地更改,所以我只想将本地 mk 分支指针移动到获取的远程分支指针,所以我执行以下操作:

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

操作似乎成功 show-ref:

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

但是,提交 a885dad 未签出到工作目录。为什么?可以自动结帐吗?

请注意,如“Merge, update, and pull Git branches without using checkouts”中所述,一个简单的 git fetch -u origin mk:mk 会做同样的事情(对于快进合并)。

问题是:这些命令是否更新了 HEAD?

如果是 git update-ref,则需要一个单独的 git update-ref -m "Updating mk to remote ..." HEAD refs/heads/mk

但是在 git fetch 中使用 -u 应该也允许更新 HEAD:

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

请注意,这些命令不会更新工作树。
您仍然需要一个单独的 git checkout -- ..