修复Git分离的头部? [英] Fix a Git detached head?
问题描述
我在存储库中做了一些工作,发现一个文件有本地更改。我不再需要它们,所以我删除了该文件,认为我只需签出新的副本即可。我想要做相当于
svn的git。
使用 git pull
似乎没有工作。一些随机搜索导致我到一个网站,有人建议做
git checkout HEAD ^ src /
( src
是包含已删除文件的目录)。
现在我发现我有一个独立的头。我不知道那是什么。如何撤消?
分离的头意味着您不再处于分支中,单一提交历史记录(在这种情况下,在HEAD之前提交,即HEAD ^)。
如果你想删除与分离的HEAD
您只需检出您所在的分支,例如
git checkout master
下一次您更改文件并希望将其恢复到它在索引中的状态时,请不要先删除文件,只需执行
git checkout - path / to / foo
将文件foo恢复到它在索引中的状态。
如果您希望保留与分离的HEAD相关联的更改
- 运行
git log -n 1
;这将在分离的HEAD上显示最近的提交。复制并粘贴提交哈希。
- 运行
git checkout master
- 运行
git branch tmp< commit-hash>
。这会将您的更改保存在名为tmp
的新分支中。 - 如果您想将所做的更改合并到
master
,从master
分支运行git merge tmp
。在运行git checkout master
之后,您应该在master
分支上。
>
I was doing some work in my repository and noticed a file has 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 .
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
is the directory containing the deleted file).
Now I find out I have a detached head. I have no idea what that is. How can I undo?
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^).
If you want to delete your changes associated with the detached 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.
If you want to keep your changes associated with the detached HEAD
- Run
git log -n 1
; this will display the most recent commit on the detached HEAD. Copy-and-paste the commit hash. - Run
git checkout master
- Run
git branch tmp <commit-hash>
. This will save your changes in a new branch calledtmp
. - 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屋!