Home > OS >  Git side branch overrode main branch?
Git side branch overrode main branch?

Time:06-15

I am not sure if I am using Git incorrectly, but I have a main branch. I wanted to test a new feature so I created a side branch. I ran

git branch new-feature
git checkout new-feature

Then, I modified my code in the new-feature branch. I realized this new feature wasn't very good, so I decided to switch back to my main branch and delete the new-feature branch.

I ran a git checkout main. However, my code did not update back to what it was before. Additionally, all the modules I modified now has an M beside them. I am wondering how I can restore my main branch.

Specifically, when I checkout the main branch, I get:

Switched to branch 'main'
M        Module1.txt
M        Module2.txt...

CodePudding user response:

You are using Git correctly.

  1. You created (and switched to) a new branch.
  2. You modified some files.
  3. You did not commit the changes. That means those changes aren't on any branch.
  4. When you switched back to the main branch, git didn't discard those changes because they didn't belong to any branch.

The files show up as modified (that's what the M means) because they still contain your changes. If you want to discard those changes:

git checkout Module1.txt Module2.txt

This will replace the files with the current version from the main branch.

  • Related