如果有人结账:
git checkout 760ac7e
例如b9ac70b
,如何在不知道SHA1的情况下返回最后一个已知头b9ac70b
?
答案 0 :(得分:237)
如果您记得以前签出了哪个分支(例如master
),您可以简单地
git checkout master
退出分离的HEAD 状态。
一般来说:git checkout <branchname>
会让你离开。
如果您不记得最后一个分支名称,请尝试
git checkout -
这也试图检查你上次检出的分支。
答案 1 :(得分:15)
使用git reflog
查找以前签出过的提交的哈希值。
用于到达上次签出分支的快捷命令(不确定这是否与分离的HEAD和中间提交一起正常工作)是git checkout -
答案 2 :(得分:1)
我有这个边缘案例,在那里我检查了我的文件目录结构不同的代码的先前版本:
git checkout 1.87.1
warning: unable to unlink web/sites/default/default.settings.php: Permission denied
... other warnings ...
Note: checking out '1.87.1'.
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 50a7153d7... Merge branch 'hotfix/1.87.1'
在这种情况下,您可能需要使用--force(当您知道返回原始分支并放弃更改是安全的事情时)。
git checkout master
无效:
$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
web/sites/default/default.settings.php
... other files ...
git checkout master --force
(或git checkout master -f
)工作:
git checkout master -f
Previous HEAD position was 50a7153d7... Merge branch 'hotfix/1.87.1'
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
答案 3 :(得分:0)
您可能在detached HEAD
状态下进行了一些新的提交。我相信,如果您按照其他答案的建议去做,
git checkout master
# or
git checkout -
然后您可能会丢失提交!! 相反,您可能需要这样做:
# you are currently in detached HEAD state
git checkout -b commits-from-detached-head
,然后将commits-from-detached-head
合并到所需的任何分支中,这样就不会丢失提交。