【发布时间】:2019-02-06 23:08:05
【问题描述】:
我正在一个本地分支 foo 工作,该分支拥有丰富而有用的历史。我做了一堆更改并提交它们。 git status 说:
On branch foo
Your branch is ahead of 'origin/foo' by 1 commit.
(use "git push" to publish your local commits)
所以我继续输入git push。
这似乎工作得很好。快速git status 透露:
On branch foo
Your branch is up to date with 'origin/foo'.
nothing to commit, working tree clean
我切换到我的本地主主分支(称为 feature1)git checkout feature1。没问题。然后我 git pull 添加我所有同事的更改。
现在我想切换回 foo 以合并我刚刚拉入 foo 的 feature1 更改。
编辑(我在打字和发帖时都错过了这个!这是关键!!!)
git checkout origin\foo
你瞧!我收到了这条我从未见过的消息:
Note: checking out 'origin/foo'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at f5a29b1083 cosmetic changes for code review
现在git status 的结果是
编辑(更正):
HEAD detached at origin/foo
nothing to commit, working tree clean
所以我的问题是多方面的:
1) 我做错了什么?这是我长期以来一直在做的事情,但以前从未发生过这样的事情。
2) 如何确保不再发生这种情况?
3) 当我将来将 foo 合并到 feature1 时,如何在不丢失我的工作和(更重要的是)不污染每个人的历史的情况下解决这个问题?
【问题讨论】:
-
消息 "checking out 'origin/foo'" 如果本地分支
foo不存在但有一个(并且只有一个)远程跟踪分支foo(在这种情况下为origin/foo)。但在这种情况下,它也应该创建本地分支foo。如果您签出除分支以外的任何内容(提交哈希、标签、HEAD~1等相对引用),您最终可能会处于“分离的 HEAD”状态。但是,我不知道为什么您当地的分支机构突然消失了。您的回购可能会以某种方式损坏。运行git fsck找出答案。 -
执行
git for-each-ref '**/foo'并包含结果? -
在
git checkout feature1之后运行git pull有没有发生什么异常? -
@axiac
git fsck揭示了一些悬空的 blob、提交和树。太多了,甚至无法显示。
标签: git