【发布时间】:2015-05-02 19:54:32
【问题描述】:
我正在编写一个切换到指定提交的 shell 脚本,复制/tmp 中的索引,将HEAD 重置为原始位置,然后在临时目录中运行命令:
orig_head=$(git rev-parse -q HEAD) # "refs/heads/master"
git checkout "$1"
# copy index to /tmp/...
git checkout "$orig_head"
# run command in /tmp/...
但是,这个脚本给了我与运行git checkout refs/heads/master 时相同的“'分离 HEAD'状态”消息:
Note: checking out 'refs/heads/master'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at c82ad67... Use vector and binary search for dictionary
如何正确存储和恢复HEAD的位置?
我宁愿避免使用reset HEAD@{1},因为这看起来很乱。
【问题讨论】:
-
如果省略“--symbolic”会发生什么?使用“--symbolic”,它似乎返回了无意义的标签“HEAD”,这在您结帐“$1”后没有任何意义。我无法重现
$(git rev-parse --symbolic -q HEAD)返回“refs/heads/master”的行为,但是... -
@AndreyTyukin:谢谢,你是对的。使用
--symbolic它根本不起作用,但没有--symbolic我会收到上面显示的消息。 -
有趣,我读了它(没有想太多),好像它在上面使用
git symbolic-ref,实际上应该使用它(见下面我的答案)。 -
是的,很奇怪。看来我的潜意识是在正确的轨道上!
标签: git git-checkout