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 plain copy print ?
  1. ref: refs/heads/master  
当然也可指向其他索引文件,不管怎么样,这个索引文件的内容又由git reset控制。

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

[plain]  view plain copy print ?
  1. $ git branch -v  
  2. * master 1aea8d9 [ahead 1] add test file x  

2.最简单用法

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

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

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

[plain]  view plain copy print ?
  1. git branch -a  
  2. * master  
  3.   remotes/origin/HEAD -> origin/master  
  4.   remotes/origin/develop  
  5.   remotes/origin/issue_193  
  6.   remotes/origin/issue_210  
  7.   remotes/origin/master  



3.detached HEAD

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

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


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

[plain]  view plain copy print ?
  1. $ git branch -v  
  2. * master 89f8dae [ahead 2] update x  
  3. $ git checkout 89f8dae^  
  4. Note: checking out '89f8dae^'.  
  5.   
  6. You are in 'detached HEAD' state. You can look around, make experimental  
  7. changes and commit them, and you can discard any commits you make in this  
  8. state without impacting any branches by performing another checkout.  
  9.   
  10. If you want to create a new branch to retain commits you create, you may  
  11. do so (now or later) by using -b with the checkout command again. Example:  
  12.   
  13.   git checkout -b new_branch_name  
  14.   
  15. HEAD is now at 1aea8d9... add test file x  


好,现在恢复回来。

[plain]  view plain copy print ?
  1. $ git checkout master  
  2. Previous HEAD position was 1aea8d9... add test file x  
  3. Switched to branch 'master'  
  4. 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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
git checkout HEAD命令的作用是将当前工作目录重置为最新的提交状态,即将HEAD指针指向最新的提交。它与git checkout Mx命令的功能相同,都是将本地仓库更新到最新的提交状态。当使用git checkout HEAD命令时,会处于"detached HEAD"状态,可以浏览、尝试修改并提交实验性更改,而且可以在不影响任何分支的情况下丢弃在此状态下进行的任何提交。如果需要创建一个新的分支来保留所创建的提交,可以再次使用带有checkout命令的-b选项来创建新分支。以git checkout head为例,如果我们尚未离开commit f,下面命令中的任何一个都将创建对它的引用:git checkout -b foo (1)、git branch foo (2)、git tag foo (3)。123 #### 引用[.reference_title] - *1* *2* [git checkout之一 HEAD基本detached 状态](https://blog.csdn.net/fduffyyg/article/details/83542504)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *3* [GIT命令git-checkout的分离HEAD](https://blog.csdn.net/ystyaoshengting/article/details/104074499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值