Git merged without me asking? Why?
I was working on master, finished up what I needed to do, then did
git commit -am "message".
I wanted to test out writing a new feature, so I did:
git branch NewFeature
followed by
git checkout NewFeature
I then made changes to version controlled files, came back to git and did
git checkout master
What I forgot to do was commit those changes to the NewFeature branch. My fault, yes, but looking around on SO it seems like that should have failed without the -f flag. Unfortunately, it just merged my changes with master. Naturally I 开发者_如何学JAVAfreaked out and did
git reset --hard head
It seems like I lost all the work I had done on the NewFeature branch when I switched back as well! What did I do wrong?
This is normal behavior.
If you don't have conflicting changes between branches, git simply "moves" the changes over when you checkout. Git should show you a summary of the moved files after the checkout.
Doing git reset --hard HEAD
will have reverted your changes.
More info
If you have uncommitted changes but you want to work on other things in a different branch, you can stash your changes for later. See stash docs.
git stash
git checkout <branch>
- Make your changes and commit
git checkout <oldBranch>
git stash pop
to un-stash
You might want to consider looking at 'git reflog' to find out the state of the branches in order to restore you changes.
But as you noted, git reset --hard
is an operation which changes the working tree, as noted in the man page:
--hard
Resets the index and working tree. Any changes to tracked files
in the working tree since <commit> are discarded.
I wrote more about the reflog in my weekly Git Tip of the Week series here:
http://alblue.bandlem.com/2011/05/git-tip-of-week-reflogs.html
精彩评论