敲了一天的代码,眼花缭乱,特别是即将放假的时刻,时间更加漫长。
总算临时结束一个阶段,快速敲打能带来唯一成就感的一句话。git push。enter!!!越响越高兴。
霎那间,傻了眼,
detached HEAD...
不明白,只知道push失败了。分支有问题?
马上git branch 看一下,晕,没有在任何分支上。于是马上切换到本地分支:
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
4cc345f fix the *** bug
If you want to keep them by creating a new branch, this may be a good time
to do so with:
git branch new_branch_name 4cc345f0573f7bf120a5f5915e4c617076d4e94a
Switched to branch '***'
不知道是什么意思,也没去理会,马上又来个git push:
To*******************************************
! [rejected] **** ->**** (non-fast-forward)
error: failed to push some refs to ***********************
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
看到这里就知道问题大了。马上ls:
然后。。。什么东西什么文件都没有,空空的。惨了,我一天的东西都没了,难道要我重新pull下来修改?
于是一阵胡搞,git log,git branch,知道的命令都敲一边,还是空空如也。。。
还好git这么霸气。google一下就OK了:http://sitaramc.github.com/concepts/detached-head.html
E文网址,看肯定不能看懂,不过起码还能看出个基本意思。
detached HEAD explained
Git has something called a 'detached HEAD' that bears some explanation. Youare told 'do not commit on a detached HEAD', and it is not always clear whator why this is.
开始主要讲git的原理,讲HEAD指针。反正,最重要的东西在最后:
git reflog show HEAD@{now} -10
dcd215b... HEAD@{5 minutes ago}: commit (amend): 0-terminology: the malloc analogy added, plus
5ce8bfe... HEAD@{11 minutes ago}: commit: 0-terminology: the malloc analogy added, plus
3d93420... HEAD@{11 minutes ago}: rebase -i (pick): updating HEAD
7fdae94... HEAD@{11 minutes ago}: checkout: moving from master to 7fdae94815d6c676742c9984132b7b9e71a57f98
3d93420... HEAD@{13 minutes ago}: rebase -i (squash): updating HEAD
c55900c... HEAD@{13 minutes ago}: rebase -i (pick): updating HEAD
7fdae94... HEAD@{13 minutes ago}: checkout: moving from master to 7fdae94815d6c676742c9984132b7b9e71a57f98
e9955c8... HEAD@{14 minutes ago}: commit: s
97ab644... HEAD@{20 minutes ago}: commit: autogen
c55900c... HEAD@{23 minutes ago}: commit (amend): 0-terminology: the malloc analogy added, plus
Now you look at this, decide which one you want, and grab it:
git branch thank_God_its_safe 7fdae94
# like 'thank_God_its_safe = 0x7fdae94815d6c676742c9984132b7b9e71a57f98'
git checkout thank_God_its_safe
里面的例子,讲得非常生动,特别分支的名字。
好了,能回到以前就OK了,马上学他git reflog show HEAD@{now} -10
然后在git branch ** ** ,最后切换到新建的分支,然后ls。终于,东西出现了。
安心了。切回我本地的分支,git merge一下。再ls看一下。原来发现世界还是这么美好。
一天的东西总算还在。
总结:在不经意间,造成了这种错误。有时有些错误还是很危险的。还好,git还是不错的。当然也不能一直全依赖它的。那篇E文还是不错的。
http://sitaramc.github.com/concepts/detached-head.html