检查出另一个分支后,无法在Git分支中找到完成的工作 [英] Can't find work done in Git branch after checking out another branch
问题描述
我在master分支上,运行 git checkout -b ui
。在一个星期的过程中做了大量的工作,在我跑完的时候:
git add。
git commit -am'ui'
git checkout master
git merge ui
git push
然后我切换到一个我曾经工作过的分支 - git checkout uploads
- 在哪一点上,在UI部门完成的任务已经失败。它没有在 git log
中列出,并且 git reset --hard HEAD ^
将我带回到分支之前'ui'分支。这是怎么发生的?
通过查看reflog并恢复到'ui'提交,我能够恢复大部分内容,但我不知道是什么原因导致了这种情况发生?
它没有丢失,只是当你在git中切换分支时(使用 git checkout
)您的工作树已完全更改为具有该分支上的文件。如果您运行:
git分支
...您应该看到 master
, ui
和上传
全部列出。
如果您想查看 ui
你应该运行:
git log ui
原因是 git reset --hard HEAD ^
让您回到 master
合并之前 ui
是您的合并提交(其中 master
所在的位置)有两个父母:
$ b
-
master
>ui
合并前
当您添加一个 ^
指向一个提交名称,它可以让您回到第一个父指向第二个父指针,您可以使用 HEAD ^ 2
。不过,我想你并不是真的想在这里改变 git reset --hard
来改变 master
的位置。 (无论如何,这是一个应该谨慎使用的命令,因为它会抛弃未提交的更改。)
无论如何,您所做的所有更改在 ui
分支上做的事情现在被合并到 master
中,所以如果你对你在那里做的事情感到满意,你应该您不必担心 ui
分支。 Greg Hewgill 的回答暗示了一些行动方案,如果你现在想要的是确保你的 uploads
分支包含所有这些工作。
I was on the master branch, and I ran git checkout -b ui
. Did tons of work over the course of a week, at the end of which I ran:
git add .
git commit -am 'ui'
git checkout master
git merge ui
git push
I then switched to a branch that I had worked on previously- git checkout uploads
- At which point every thing I had done in the ui branch was lost. It's not listed in git log
, and git reset --hard HEAD^
takes me back to the branch prior to the 'ui' branch. How does this happen?
I was able to recover most of it by looking through reflog and reverting to the 'ui' commit, but I wonder what causes this to happen?
It wasn't lost, it's just that when you switch branches in git (with git checkout
) your working tree is completely changed to have the files on that branch. If you run:
git branch
... you should see master
, ui
and uploads
all listed.
If you want to see the log for the ui
branch, you should run:
git log ui
The reason that git reset --hard HEAD^
moved you back to the commit on master
before you merged ui
is that your merge commit (where master
was at) has two parents:
master
before the mergeui
before the merge
When you add a single ^
to a commit name, that takes you back to the first parent - to refer to the second parent, you would use HEAD^2
. However, I guess that you don't really want to do git reset --hard
here to change where master
is. (In any case, that's a command that should be used with care, since it will throw away changes that aren't committed.)
Anyway, all the changes that you did on the ui
branch are now merged into master
, so if you're happy with what you did there you shouldn't need to worry about the ui
branch any more. Greg Hewgill's answer suggests a couple of courses of action if what you now want is to make sure that your uploads
branch contains all that work.
这篇关于检查出另一个分支后,无法在Git分支中找到完成的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!