So, I'm a bit confused.
I'm not very good on git, but I remember that if you're in a branch with uncommited changes and you try to checkout another branch, git will either don't let you, or it will discard your changes.
I've also checked this behaviour to be true in the Apress' "Pro Git" book by Chacon and Straub.
And also
I confess my English isn't very good, but I remember this interpretation to be true from the old days were I used to use git more often. Or did I'm wrong about git discarding uncommited changes when changing branch? Am I understanding what the book is saying wrongly?
If I'm right, why do my repository is moving changed files from one branch to another? You can check the behaviour here:
Check that I'm in main branch and that it's clean from changes using
git status
.
Create a new branch and move to it.
Do some random alterations like deleting a couple of lines using
ed
.
Change back to main branch.
Surprise, a
git diff
or agit status
shows that the file is changed on main as well.
Should not git prevent me changing branch or discard my changes instead? This is very confusing and took me a while to realize why my main branch was having unexpected behaviour if I didn't changed anything on it.
git version 2.30.1 (Apple Git-130)
Edit: Some answers said about git switch
being different than git checkout
. I'm still not sure that's the case since different people (and docs) says different things.
But, I've also tried the same using git checkout
and it will also move the files instead of preventing me to switch or warning me about differences in the working directory.
You can see git checkout
doing the same as follow: