0

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...
Rui Nian
  • 2,544
  • 18
  • 32
  • You'll find that Git will sometimes let you switch from one branch to another and carry changed files around, and yet, sometimes an attempt to switch from one branch to another will object, saying that this would lose your changes. The ability to switch while carrying changes around is a feature, albeit a surprising one with limits. For much more about these limits, see [Checkout another branch when there are uncommitted changes on the current branch](https://stackoverflow.com/q/22053757/1256452). – torek Jun 15 '22 at 09:38

1 Answers1

1

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.

larsks
  • 277,717
  • 41
  • 399
  • 399