2

I made some changes in my master branch and without committing them, I checked out to development branch. I was expecting an error to be thrown but instead of that, my changes in master branch are merged with development branch.

$ git checkout development
Switched to branch 'development'
M       pom.xml
Your branch is up-to-date with 'origin/development'.

Instead of this I was expecting the below error, so that I could stash or commit my changes before checking out:

error: You have local changes ....; cannot switch branches.

Does anybody know why it happened or how can I prevent it to happen again?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
leventunver
  • 3,269
  • 7
  • 24
  • 39
  • To be more precise, my checkout command in the first hand was a mistake and I shouldn't have done that. That's why I was expecting an error to be thrown. Is this the default behaviour of git? – leventunver Aug 15 '17 at 13:09
  • Yes, AFAIK it is the default behavior of git. – von Oak Aug 15 '17 at 13:12
  • 1
    The changes in the working tree are not part of any branch unless you commit them. When you checkout another branch, Git preserves them if they do not conflict with the content of the affected files in the new branch. – axiac Aug 15 '17 at 13:18
  • @axiac yeah that makes sense. – leventunver Aug 15 '17 at 13:34
  • 1
    See also https://stackoverflow.com/q/22053757/1256452 (not sure if this question counts as a duplicate of that one) – torek Aug 15 '17 at 14:12
  • @torek it can be considered as duplicate. Your answer in that question delivers an exceptional insight. And there is no such thing as "default behavior" it seems. It depends on the situation. My question should be flagged as duplicate of that question, so whenever anyone hits this question can see your answer. But 50 votes needed for that so it may never happen. Still, thanks for passing by. Cheers! – leventunver Aug 15 '17 at 15:04

1 Answers1

3

As far as I know, this is now the default behavior of Git. When switching branches, you carry your working directory with you. I also seem to recall earlier versions where this was not the case.

You might not be able to avoid this from happening, but you can always make sure your working directory (and stage) is clean before switching branches. Or, you could make a commit with your work, or stash.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    ...and the reason it's the default behavior is that `git` assumes you don't want to lose work just because you switched branches. For example, you start editing a file on `master`, and later realize you should have opened a feature branch first. – larsks Aug 15 '17 at 13:16
  • ...and this is very easy to rollback - just `checkout ` and you are back where you were. – instantepiphany Aug 15 '17 at 13:29
  • @larsks that's why I was expecting an error. By this way, you don't lose your work but you can't switch your branches either, unless you take some action by committing or stashing. That's how I remember like Tim. axiac's comment also makes sense too, since the working tree does not belong anywhere, it shouldn't be a problem then. I can go back to my master branch and commit them anytime I want. – leventunver Aug 15 '17 at 13:30
  • @larsks now I get your point. I think this default behavior is better for people who forgot to create a branch at first. So it's clear. Thanks everyone for your ideas and comments. – leventunver Aug 15 '17 at 13:32