如何修复Git分离头? [英] How do I fix a Git detached head?

查看:47
本文介绍了如何修复Git分离头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在存储库中进行了一些工作,发现文件有本地更改.我不再想要它们了,于是我删除了文件,以为我可以签出一个新副本.我想做的Git等同于

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.

  1. 运行 git branch tmp -这会将您的更改保存在名为 tmp 的新分支中.
  2. 运行 git checkout master
  3. 如果要将所做的更改合并到 master 中,请从 master 分支运行 git merge tmp .运行 git checkout master 后,您应该在 master 分支上.
  1. Run git branch tmp - this will save your changes in a new branch called tmp.
  2. Run git checkout master
  3. If you would like to incorporate the changes you made into master, run git merge tmp from the master branch. You should be on the master branch after running git checkout master.

这篇关于如何修复Git分离头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆