开发者

Can't find work done in Git branch after checking out another branch

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:

  1. master before the merge
  2. ui 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.


Everything you've described is as expected. It sounds like you were surprised that your work on the ui branch did not appear when you switched to the uploads branch. That makes sense, because the ui work did not exist at the time you created uploads. You can take two approaches here:

  1. Merge master into uploads: git merge master
  2. Rebase uploads onto master: git rebase master

Both these commands would be run on the uploads branch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜