美文网首页
代码丢失,git提交代码出现Detached HEAD解决

代码丢失,git提交代码出现Detached HEAD解决

作者: 月圆星繁 | 来源:发表于2020-06-27 23:47 被阅读0次

    端午在家改代码,项目从通过git拉取下来之后,切换到了自己开发的远程分支,有提示,但是自己没有仔细看:

    $ git checkout origin/meimei
    Note: checking out 'origin/meimei'.
    
    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 11aaaf4d Merge remote-tracking branch 'remotes/origin/develop'
     in to meimei
    

    没有注意到 'detached HEAD' ,之后又用的是sublime开发的(phpstorm过期了),开发完成之后也没有什么。
    到了提交代码的时候不想用git cmd去提交,就想把phpstorm给激活了去提交,毕竟一直用的这个比较熟悉(还是对git内容不是很熟练啊)

    第一次提交,代码却丢失

    在phpstorm重新激活后,还是处于detached HEAD,我用它commit,然后一直弹框提示,我像往常一样一直点commit,略过那些TODO、warms之类的信息。然后切换分支,准备合并代码push上去。发现没有commit的内容。

    发现修改的代码全部丢失之后,我的第一感觉就是我commit了,肯定还在那里,不要被覆盖了。
    然后我还在idea里面瞎点rollback,各种git log、git status命令查看。
    在git log之后我知道了自己的初始拉取下来的HEAD(即拉取下来的commitId),然后checkout 到这个分支。

    总结:自己对git命令不熟悉,加上粗心,导致代码

    找回代码,重新提交

    我没有使用git的命令去找回代码,一是对git有些命令不熟悉,在公司都是idea提交、拉取、推,所以我使用的是phpstorm里面的local history的名对文件修改部分进行找回。

    在这个分支上,索性我知道我修改的几个文件,慢慢的全部找回来了。然后执行git status,这次才仔细看了出现的内容:

    H:\phpstudy_pro\WWW\ccccc>git status
    HEAD detached at 11aaaf4d
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   Apps/Admin/Controller/MemberController.class.php
            modified:   Apps/Admin/View/Member/lists.html
            modified:   Apps/Common/Common/function.php
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            "Apps/Admin/Controller/MemberController.class - 
    \345\211\257\346\234\254.php"
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    然后就大概看了下,以为是要我提交,然后用就idea去提交,一样的略过一些 todo item之类的信息,但是这次明显要认真些。
    在其中一步,弹框提示:

    The Git repository at the following path is in the detached HEAD state: H:\phpstudy_pro\WWW\ccccc You can look around, make experimental changes and commit them, but be sure to checkout a branch not to lose your work. Otherwise you risk losing your changes. Read more about detached HEAD.

    然后才发现问题的所在。

    解决:

    1.在当前的detached head的状态 :
    git checkout -b temp 创建临时分支

    1. add / commit 提交修改代码到临时分支

    3.git checkout master 切回主分支

    4.git merge temp 拉取临时分支代码

    5.git branch -d temp 删除临时分支

    相关文章

      网友评论

          本文标题:代码丢失,git提交代码出现Detached HEAD解决

          本文链接:https://www.haomeiwen.com/subject/ebebfktx.html