Git:如何从“分离的HEAD”状态返回

如果你要签出一个分支:

git checkout 760ac7e

从例如b9ac70b ,如何能不知道它的SHA1回到最后已知的头b9ac70b

如果你记得之前检查过哪个分支(例如master ),你可以简单的

 git checkout master 

脱离HEAD状态。

一般来说: git checkout <branchname>会帮你解决这个问题。

如果您不记得最后一个分支名称,请尝试

 git checkout - 

这也试图检查出你最后检查出来的分支。

使用git reflog查找以前签出的提交的散列。

一个快捷命令到达你最后一个签出分支(不知道这是否正常与分离HEAD和中间提交虽然)是git checkout -

我有这个边缘情况下,我检查了我的文件目录结构是不同的代码的以前的版本:

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