如何修复 Git 分离头? [英] How do I fix a Git detached head?
问题描述
我正在我的存储库中做一些工作,并注意到一个文件有本地更改.我不再想要它们,所以我删除了文件,以为我可以签出一个新的副本.我想做相当于
I was doing some work in my repository and noticed a file had local changes. I didn't want them anymore so I deleted the file, thinking I can just checkout a fresh copy. I wanted to do the Git equivalent of
svn up .
使用 git pull
似乎不起作用.一些随机搜索将我带到了一个有人推荐做的网站
Using git pull
didn't seem to work. Some random searching led me to a site where someone recommended doing
git checkout HEAD^ src/
(src
是删除文件所在的目录).
(src
is the directory containing the deleted file).
现在我发现我有一个分离的头.我不知道那是什么.如何撤消?
推荐答案
分离头意味着你不再在一个分支上,你已经检出了历史中的一个提交(在这种情况下是 HEAD 之前的提交,即 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
这会将文件 foo 恢复到它在索引中的状态.
This will restore the file foo to the state it is in the index.
- 运行
git branch tmp
- 这会将您的更改保存在名为tmp
的新分支中. - 运行
git checkout master
- 如果您想将所做的更改合并到
master
中,请从master
分支运行git merge tmp
.运行git checkout master
后,你应该在master
分支上.
- Run
git branch tmp
- this will save your changes in a new branch calledtmp
. - Run
git checkout master
- If you would like to incorporate the changes you made into
master
, rungit merge tmp
from themaster
branch. You should be on themaster
branch after runninggit checkout master
.
这篇关于如何修复 Git 分离头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!