Git:如何从“分离头”状态返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11801071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: How to return from 'detached HEAD' state
提问by James Raitsev
If one would checkout a branch:
如果有人要结帐一个分支:
git checkout 760ac7e
git checkout 760ac7e
from e.g. b9ac70b
, how can one go back to the last known head b9ac70b
without knowing its SHA1?
例如b9ac70b
,如何在b9ac70b
不知道其 SHA1 的情况下返回到最后一个已知的头部?
回答by eckes
If you remember which branch was checked out before (e.g. master
) you could simply
如果您记得之前检出哪个分支(例如master
),您可以简单地
git checkout master
to get out of detached HEADstate.
摆脱分离的 HEAD状态。
Generally speaking: git checkout <branchname>
will get you out of that.
一般来说:git checkout <branchname>
会让你摆脱那个。
If you don't remember the last branch name, try
如果您不记得最后的分支名称,请尝试
git checkout -
This also tries to check out your last checked out branch.
这也尝试检查您上次检出的分支。
回答by knittl
Use git reflog
to find the hashes of previously checked out commits.
使用git reflog
发现以前签出的提交的哈希值。
A shortcut command to get to your last checked out branch (not sure if this work correctly with detached HEAD and intermediate commits though) is git checkout -
到达最后一个签出分支的快捷命令(但不确定这是否与分离的 HEAD 和中间提交一起正常工作)是 git checkout -
回答by mcaleaa
I had this edge case, where I checked out a previous version of the code in which my file directory structure was different:
我有这个极端情况,我检查了以前版本的代码,其中我的文件目录结构不同:
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'
In a case like this you may need to use --force (when you know that going back to the original branch and discarding changes is a safe thing to do).
在这种情况下,您可能需要使用 --force(当您知道返回原始分支并放弃更改是安全的做法时)。
git checkout master
did not work:
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
(or git checkout master -f
) worked:
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'.
回答by tipsy boopenstein
You may have made some new commits in the detached HEAD
state. I believe if you do as other answers advise:
您可能在该detached HEAD
州进行了一些新的提交。我相信如果你按照其他答案的建议去做:
git checkout master
# or
git checkout -
then you may lose your commits!! Instead, you may want to do this:
那么你可能会失去你的提交!!相反,您可能想要这样做:
# you are currently in detached HEAD state
git checkout -b commits-from-detached-head
and then merge commits-from-detached-head
into whatever branch you want, so you don't lose the commits.
然后合并commits-from-detached-head
到你想要的任何分支,这样你就不会丢失提交。