我怎么最终得到一个独立的HEAD? [英] How did I end up with a detached HEAD?

查看:158
本文介绍了我怎么最终得到一个独立的HEAD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从master检查了一个提交/分支,然后检查回master并写了一些东西。在那之后,我承诺了,但我最终得到了一个独立的HEAD。为什么?



这是我做的:


  1. 创建一个新项目创建git存储库。

  2. git add

  3. git commit

  4. 输入一些单词

  5. git commit

  6. 结账至上一次提交

  7. 结账后退出



    < img src =https://i.stack.imgur.com/NDpUK.pngalt =step7>


  8. 输入一些单词


  9. 尝试提交;它提示有一个独立的头。




IntelliJ IDEA的控制台显示:

  17:08:58.143:cd C:\ Users\jiahao\IdeaProjects\testtt\src 
17:08:58.143:git init
初始化的空Git存储库在C:/Users/jiahao/IdeaProjects/testtt/src/.git/
17:09:16.331:cd C:\ Users \jiahao\IdeaProjects\testtt\src
17 :09:16.331:git -c core.quotepath = false add --ignore-errors - C.java
17:09:24.407:cd C:\ Users\jiahao\IdeaProjects\testtt \\ src
17:09:24.407:git -c core.quotepath = false commit --only -FC:\ Users\jiahao \ AppData \ Local \Temp\git-commit-msg- 0.txt - C.java
[master(root-commit)22d1c79] first commit
1 file changed,6 insertions(+)
create mode 100644 C.java

17:09:38.060:cd C:\ Users \jiahao\IdeaP rojects\testtt\src
17:09:38.060:git -c core.quotepath = false commit --only -FC:\ Users \jiahao\AppData \ Local \Temp\git -commit-msg-0.txt - C.java
[master 69084f3] second commit
1 file changed,1 insert(+)

17:09:44.136: CD C:\Users\jiahao\IdeaProjects\testtt\src
17:09:44.136:混帐-c core.quotepath =假结账22d1c7919eab50925411d9bbb8a9ad1575608c27
注:检查出 '22d1c7919eab50925411d9bbb8a9ad1575608c27'。
您处于'独立的HEAD'状态。您可以环顾四周,进行实验性
更改并提交它们,并且您可以放弃在此
状态中进行的任何提交,而不会通过执行另一次结帐而影响任何分支。
如果你想创建一个新的分支来保留你创建的提交,你可以再次使用-b和checkout命令
这样做(现在或以后)。示例:
git checkout -b< new-branch-name>
HEAD现在是22d1c79 ...首先提交
17:09:46.576:cd C:\ Users \jiahao \ IdeaProjects\testtt\src
17:09: 46.576:git -c core.quotepath = false checkout 69084f344b79a48da92855d3fb633a28a672a302
上一个HEAD位置是22d1c79 ...第一次提交
HEAD现在是69084f3 ...第二次提交
17:18:26.999: cd C:\ Users\jiahao\IdeaProjects\testtt\src
17:18:26.999:git -c core.quotepath = false commit --only -FC:\ Users\jiahao \\ \\ APPData \本地\ Temp \ git-commit-msg-0.txt - C.java
[分离HEAD 783fbf2]第三次提交
1文件已更改,1次插入(+)


解决方案

问题和解决方案



仔细检查IntelliJ-IDEA日志可以发现问题的本质。在创建前两个提交后,您遇到以下情况:





不知怎的,你检查了你的第一次提交,这使你处于分离-HEAD状态:





< hr>

你写道:


我从<$ c $检出了一个提交/分支c> master [...]


小心。检查恰好是分支的提示是等同于检出该分支!






请注意, HEAD 现在直接指向提交,指向分支。这就是分离的HEAD的定义。 HEAD 指向与 master 相同的提交这一事实不会改变您的HEAD已分离的事实。






某些IDE可能无法清楚地表明您实际上处于分离-HEAD状态。



要退出分离的HEAD状态,您需要将 HEAD 重新附加到分支( master ,这里)。取决于你想做什么。在下面,我假设您可以访问Git CLI:




  • 如果您要放弃第三次提交,只需运行

      git checkout master 

    你最终会回到这种情况:




  • 如果您想保留第三次提交并让 master 指向它,请运行

      git branch -f master HEAD 



    然后

      git checkout master 




分手提示



在IDE中学习Git是灾难的配方。您应首先在命令行中建立您的理解,然后才开始在GUI中使用Git,如果您认为这样做会改善您的工作流程。


I checked out a commit/branch from master, and then checked out back to master and wrote something. After that, I committed it, but I ended up with a detached HEAD. Why?

Here is what I did:

  1. Create a new project and create git repository.
  2. git add
  3. git commit
  4. type some words
  5. git commit
  6. checkout to previous commit
  7. checkout back

  8. type some word

  9. try to commit; it prompts there is a detached head.

IntelliJ IDEA's console shows:

17:08:58.143: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:08:58.143: git init
Initialized empty Git repository in C:/Users/jiahao/IdeaProjects/testtt/src/.git/
17:09:16.331: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:16.331: git -c core.quotepath=false add --ignore-errors -- C.java
17:09:24.407: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:24.407: git -c core.quotepath=false commit --only -F C:\Users\jiahao\AppData\Local\Temp\git-commit-msg-0.txt -- C.java
[master (root-commit) 22d1c79] first commit
 1 file changed, 6 insertions(+)
 create mode 100644 C.java

17:09:38.060: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:38.060: git -c core.quotepath=false commit --only -F C:\Users\jiahao\AppData\Local\Temp\git-commit-msg-0.txt -- C.java
[master 69084f3] second commit
 1 file changed, 1 insertion(+)

17:09:44.136: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:44.136: git -c core.quotepath=false checkout 22d1c7919eab50925411d9bbb8a9ad1575608c27
Note: checking out '22d1c7919eab50925411d9bbb8a9ad1575608c27'.
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 22d1c79... first commit
17:09:46.576: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:46.576: git -c core.quotepath=false checkout 69084f344b79a48da92855d3fb633a28a672a302
Previous HEAD position was 22d1c79... first commit
HEAD is now at 69084f3... second commit
17:18:26.999: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:18:26.999: git -c core.quotepath=false commit --only -F         C:\Users\jiahao\AppData\Local\Temp\git-commit-msg-0.txt -- C.java
[detached HEAD 783fbf2] third commit
1 file changed, 1 insertion(+)

解决方案

Problem and solution

Close inspection of the IntelliJ-IDEA log reveals the nature of the problem. After creating your first two commits, you were in the following situation:

Somehow, you checked out your first commit, which put you in detached-HEAD state:

Then, similarly, you checked out your second commit (which happened to be the tip of your master branch). This still left you in detached-HEAD state:


You write:

I checked out a commit/branch from master [...]

Be careful. Checking out a commit that happens to be the tip of a branch is not equivalent to checking out that branch!


Note that HEAD is now pointing directly to a commit, not to a branch. That is the definition of "detached HEAD". The fact that HEAD points to the same commit as master changes nothing about the fact that your HEAD is detached.


Some IDEs may not give you a clear indication that you are in fact in detached-HEAD state. Even git log --decorate, for a long time, gave you no clue as to whether HEAD was pointing to master, or detached and pointing directly at master's tip.


You then made a third commit, which, as expected, still left you with a detached HEAD; your master branch still points to the second commit.

To get out of detached HEAD-state, you need to reattach HEAD to a branch (master, here). How depends on what you want to do. In the following, I'm assuming you have access to the Git CLI:

  • If you want to discard your third commit, simply run

    git checkout master
    

    and you'll end up back in this situation:

  • If you want to keep your third commit and make master point to it, run

    git branch -f master HEAD
    

    and then

     git checkout master
    

Parting tip

Learning Git in an IDE is a recipe for disaster. You should build your understanding at the command line first, and only then start using Git from within a GUI, if you feel that doing so would improve your workflow.

这篇关于我怎么最终得到一个独立的HEAD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆