0

Scenario:

  1. Working on branch test-bootstrap
  2. Make changes to file bootstrap.sh
  3. run git checkout master

Now when I'm on master I see my uncommitted changes from the test-bootstrap branch on master.

This is is strange to me because I whenever I used to do this in the past, I would get an error message along the lines, of "can't checkout master, please commit your changes". Essentially, I was prevented from checking out a new branch until my changes were committed.

How can I make it so that checking out a new branch when i have uncommitted changes on my current branch is prevented?

metersk
  • 11,803
  • 21
  • 63
  • 100
  • 1
    The short answer is that you can't. The good news is that if your checkout worked and you have forgotten to commit stuff, you can just switch back and commit. See the linked duplicate. – torek Apr 25 '18 at 14:40
  • 1
    FWIW, the reason why git sometimes throws an error when switching branches with uncommitted changes is because those files would have to be clobbered when updating the index. If those files had no changes between your branches (besides the uncommitted changes that you have made) then you are free to switch. Therefore in your situation, as long as you haven't made any additional changes on `master`, you are free to switch back to `test-bootstrap`. – 0x5453 Apr 25 '18 at 14:40
  • 1
    Read Torek's answer [here](https://stackoverflow.com/questions/22053757/checkout-another-branch-when-there-are-uncommitted-changes-on-the-current-branch) for an in-depth discussion of this. I also recall always seeing this behavior in earlier versions of Git. I think it changed at some point. The answer is basically that you have to always be vigilant and do `git status` before checking out another branch. – Tim Biegeleisen Apr 25 '18 at 14:40

0 Answers0