欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

git checkout之一 HEAD基本和detached 状态

发布时间:2024/4/17 编程问答 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 git checkout之一 HEAD基本和detached 状态 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

http://blog.csdn.net/csfreebird/article/details/7583363

1.HEAD基础

git checkout 实际上是修改HEAD文件的内容,让其指向不同的branch。

HEAD文件指向的branch就是当前branch.

一般来讲,HEAD的内容是指向staging(暂存区)的master文件的。

[plain] view plaincopyprint?
  • ref: refs/heads/master  
  • 当然也可指向其他索引文件,不管怎么样,这个索引文件的内容又由git reset控制。

    通过git branch命令看到的结果和HEAD文件内容一致。

    [plain] view plaincopyprint?
  • $ git branch -v  
  • * master 1aea8d9 [ahead 1] add test file x  
  • 2.最简单用法

    git checkout最简单的用法,显示工作区,暂存区和HEAD的差异:

    [plain] view plaincopyprint?
  • $ git checkout  
  • M   x  
  • Your branch is ahead of 'origin/master' by 1 commit.  
  • 意思是我本地仓库比远程仓库领先一个提交操作。git checkout HEAD 功能相同。

    如果用-a 参数,可以看到很多branch,包括远程的branch,比如:

    [plain] view plaincopyprint?
  • git branch -a  
  • * master  
  •   remotes/origin/HEAD -> origin/master  
  •   remotes/origin/develop  
  •   remotes/origin/issue_193  
  •   remotes/origin/issue_210  
  •   remotes/origin/master  



  • 3.detached HEAD

    如果让HEAD文件指向一个commit id,那就变成了detached HEAD。git checkout 可以达到这个效果,用下面的命令:

    [plain] view plaincopyprint?
  • git checkout 1aea8d9^  
  • laea8d9是最近的一次commit id,^指的是之前一次,因此上面的操作结果是让HEAD文件包含了倒数第二次提交的id.


    下面演示如何进入datached HEAD状态,并恢复回来。

    [plain] view plaincopyprint?
  • $ git branch -v  
  • * master 89f8dae [ahead 2] update x  
  • $ git checkout 89f8dae^  
  • Note: checking out '89f8dae^'.  
  •   
  • 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 1aea8d9... add test file x  

  • 好,现在恢复回来。

    [plain] view plaincopyprint?
  • $ git checkout master  
  • Previous HEAD position was 1aea8d9... add test file x  
  • Switched to branch 'master'  
  • Your branch is ahead of 'origin/master' by 2 commits.  

  • 我并不清楚detached HEAD有何实际用处,反正就是一个让HEAD随便指向某个commit id,而不在乎是哪个branch的功能。


    http://stackoverflow.com/questions/10228760/fix-a-git-detached-head

    Detached head means you are no longer on a branch, you have checked out a single commit in the history (in this case the commit previous to HEAD, i.e. HEAD^).

    You only need to checkout the branch you were on, e.g.

    git checkout master

    Next time you have changed a file and want to restore it to the state it is in the index, don't delete the file first, just do

    git checkout -- path/to/foo

    This will restore the file foo to the state it is in the index.


    总结

    以上是生活随笔为你收集整理的git checkout之一 HEAD基本和detached 状态的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。