Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merged without me asking? Why?

Tags:

git

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?

like image 444
you786 Avatar asked Jul 09 '26 03:07

you786


1 Answers

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
like image 65
adlawson Avatar answered Jul 12 '26 07:07

adlawson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!