0

I am in my branch DEV, I modify the file A.txt, when I do:

git checkout master

instead of denying me the git checkout and proposing to do a git stash, he passes my uncommitted changes

like this :

(DEV) $ git checkout master
M A.txt
(master) $ git status
modified A.txt

This is a recent problem that my colleague also has, what is it due to ? I did not have this problem before ..

Thank you

Ikbel Bmmi
  • 21
  • 2
  • 1
    This is how [`git checkout`](https://git-scm.com/docs/git-checkout#git-checkout-emgitcheckoutemltbranchgt) always worked. – axiac Jan 25 '18 at 09:42
  • Since you have no other A.txt in the master branch, git will encounter no problem switching to master branch. – mrtna Jan 25 '18 at 09:43
  • Why is it a problem for you? – Lasse V. Karlsen Jan 25 '18 at 09:47
  • before I had the same thing as @florieger, which seems to be the normal behavior. That's why now that Git's behavior is changing for me, it seems like a problem. – Ikbel Bmmi Jan 25 '18 at 10:00

3 Answers3

2

After several tests I finally understood the real functioning of GIT.

It passes my changes as long as the're not committed, but once I commit, modifications doesn't pass.

From there (after a first commit), if I make changes on these same files and I try to change branch, GIT doesn't allow me and advises me to do a Stash.

So I covered all possible cases by my tests, thank you for your answers that helped me a lot to find the solution to my problem (which was to better understand the operation of GIT).

Ikbel Bmmi
  • 21
  • 2
1

I can only reproduce this if the branches point to the same commit.
In your scenario DEV and master.

Otherwise I get a message:

error: Your local changes to the following files would be overwritten by checkout:
    Tests/ACInteractorTests/InteractorErrorTests.swift
    Tests/ACInteractorTests/LazyInteractorTests.swift
Please commit your changes or stash them before you switch branches.

It looks like a convenience feature to me that git is not complaining as long as your new branch is still pointing to the same commit as your old branch.

Note: My git GUI client, Tower for Mac, always complains.

florieger
  • 1,310
  • 12
  • 22
  • I had exactly the same thing as you, which seems to be the normal behavior. Now I do not have this error anymore, it passes my uncommitted changes without error ... – Ikbel Bmmi Jan 25 '18 at 09:57
  • @IkbelBmmi As I said before, the error is only shown when the old and new branch do *not* point at the same commit. – florieger Jan 25 '18 at 12:12
0

It is explained in the documentation of git checkout:

git checkout <branch>

To prepare for working on <branch>, switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

The checkout fails if the local changes cannot be successfully applied on the corresponding files checked out from the specified branch.

axiac
  • 68,258
  • 9
  • 99
  • 134