1

I have a git repo with several branches. Usually, if I make changes in one branch, and attempt to switch to another branch, git tells me that I have to commit or discard the changes before I can switch branch.

Today, I modified a file in branch A and typted git checkout master in order to switch to master branch. And git just, without any warning, copied all changes from branch A to the master branch.

Why does this no longer work properly?

Hugo y
  • 1,421
  • 10
  • 20
Julian
  • 64
  • 7

2 Answers2

3

You get a message asking you to stash or commit changes from git only when any of your changes have some conflicts with the changes with the branch you are switching to.

When your changes don't have any conflicts with the changes in the branch you are switching to , git won't prompt you to stash or commit changes.

However, My opinion is that you should always commit or stash your changes before you switch branch because you won't want to copy your changes over to the branch you are switching to , unless you really want to do so.

SloppyJoe
  • 393
  • 4
  • 7
2

It only asks you to commit, stash, or discard changes if checking out the branch will conflict with changes in working tree. If there's no conflict, then you will simply have changes pending and you're free to switch to any non conflicting branch.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • 1
    So why are my changes in branch B applied to my master branch when I switch to it? The file I edited in B replaced the corresponding file in the master branch. – Julian Jun 25 '16 at 14:49
  • They're actually not committed anywhere, so you've just got changes hanging out in your working tree. If you commit those to B and then checkout master, then you'll see them disappear. – Jeff Puckett Jun 25 '16 at 14:52
  • @JeffPuckett changes are not committed anywhere, but carrying changes in branch A to branch B after a `switch` without any warning can be very confusing. Say a file in B is deleted, `git switch A` will silently also delete the file in A. `git status` will show the deleted file but when several files are involved, git should let the user know. Isn't it? – calocedrus Jul 15 '22 at 07:03