3

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 freaked 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?

you786
  • 3,659
  • 5
  • 48
  • 74
  • 1
    Not answering your question, but two of your commands could be combined into `git checkout -b NewFeature`. (And if it's not exactly equivalent, someone will point it out in a few minutes.) – Keith Thompson Aug 20 '11 at 02:02

2 Answers2

6

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
Community
  • 1
  • 1
adlawson
  • 6,303
  • 1
  • 35
  • 46
0

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

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • Thanks for the edits, guys. I blame Lion for automatically changing the spelling when I wasn't looking ... – AlBlue Aug 25 '11 at 19:08