Git:如何从'独立的HEAD'状态返回

Jam*_*sev 187 git

如果有人结账分行:

git checkout 760ac7e

例如b9ac70b,如何在b9ac70b不知道SHA1的情况下回到最后一个已知头部?

eck*_*kes 289

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

git checkout master
Run Code Online (Sandbox Code Playgroud)

摆脱独立的HEAD状态.

一般来说:git checkout <branchname>会让你走出这一步.

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

git checkout -
Run Code Online (Sandbox Code Playgroud)

这也试图检查你上次检出的分支.

  • `git checkout -` - 杀手功能! (11认同)
  • @jocassid你做的.它们存在了一段时间但是当运行`git gc`时,它们将被永久删除.你可以用`git reflog`看它们,只要它们还在那里. (2认同)

kni*_*ttl 16

使用git reflog发现以前签出的提交的哈希值.

一个快捷方式命令,用于访问上一个签出的分支(不确定这是否与分离的HEAD和中间提交一起正常工作) git checkout -


小智 14

您可能在该州做出了一些新的提交detached HEAD。我相信如果你按照其他答案的建议去做:

git checkout master
# or
git checkout -
Run Code Online (Sandbox Code Playgroud)

那么你可能会失去你的承诺!相反,您可能想要这样做:

# you are currently in detached HEAD state
git checkout -b commits-from-detached-head
Run Code Online (Sandbox Code Playgroud)

然后合并commits-from-detached-head到你想要的任何分支,这样你就不会丢失提交。


mca*_*eaa 7

我有这个极端情况,我检查了以前版本的代码,其中我的文件目录结构不同:

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'
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您可能需要使用 --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 ...
Run Code Online (Sandbox Code Playgroud)

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'.
Run Code Online (Sandbox Code Playgroud)