2

I already read this question: Modified files in a git branch are spilling over into another branch and I understand (I hope) how git works.

Could you explain me why it works like that, I do not understand the mindset. I only found disadvantages but no advantage...

Community
  • 1
  • 1
Calfater
  • 1,255
  • 1
  • 10
  • 19

1 Answers1

2

The idea is to not change a file being modified without the explicit user's authorization: that is the "mindset": no unwanted "surprise".

Changing branch doesn't change by default modified files.
It actually blocks the checkout if those modified files would be overwritten during said checkout: it is up to the user to decide if those changes stay or go.

"In practical": you don't want a tool do do anything "for you". You want to use the tool the way you intent.

If you intent to clean untracked files:

The tool alone cannot decide what should be done with those untracked/modified files when switching branch.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Ok for the idea, it looks great, but in practical: I'am working on master, and I already modified some files, when I think to do a refactoring. So, I create a new branch refacto and work on it. I remove some files I previously created on master but not yet committed. The refactoring was finally not a good idea and I decide to 'revert' to master and delete the refacto branch. But in master branch uncommitted files where delete from the 'refacto' branch'. How can I do to avoid this problem? Even if I well understand the idea, I don't understand why git works like this and what are the benefits? – Calfater Aug 14 '14 at 10:43
  • 1
    @Calfater "in practical": you don't want a tool do do anything "for you". You want to use the tool the way you intent. If you intent to clean untracked files, clean the worktree: http://stackoverflow.com/a/64966/6309, or reset it with (http://stackoverflow.com/a/19032417/6309) `git checkout -- .` (or stash it http://stackoverflow.com/a/20609889/6309 if you want to reuse that work in progress later) – VonC Aug 14 '14 at 10:56