I'm in branch a
and make some modifications in the index.php
file. For example I add //test
then switch to branch b
. I open the index.php
file and see that there is that //test
in the index.php
file that was added when I was in branch a
. Is it a bug or I made a mistake? How to differentiate the codes in different branches?

- 1,612
- 3
- 18
- 57
-
Esp. this answer: https://stackoverflow.com/a/246298/7976758 – phd Dec 31 '20 at 08:02
3 Answers
There is no bug at work here, and what you are observing is the typical behavior of Git when switching branches with a dirty working directory. Actually, if some of your local changes would conflict with the newly checked out branch, Git will warn you of this. For example, if some of your changes would be lost or overwritten, Git would block the attempt to checkout the new branch.
The best practice here is to not move around branches if your working directory have changes in it. Instead, consider committing your work, or possibly making a stash. Then, once your working directory is clean, checkout the new branch.

- 502,043
- 27
- 286
- 360
-
1) I'm using PHPStorm and when switching, it doesn't warn. 2) I haven't completed the tasks yet so I think committing would not be a good choice. – kodfire Dec 31 '20 at 08:10
-
1@kodfire To your second point, that's not really true at all. You can always come back to the first branch and _amend_ the commit. To the first point, you might not get any warning at all. If checking out the new branch can be compatible with the changes in your working directory, then Git is happy to checkout silently. – Tim Biegeleisen Dec 31 '20 at 08:12
git only manages the files in your repository. Either commit the (dirty) file on your branch or stash it.

- 23,068
- 5
- 28
- 38
-
1More precisely, Git manages work-tree copies of files that are currently in Git's index, or must go into Git's index (and your work-tree) when checking out some particular commit. Note that a default `git stash` won't help here though, as `git stash` only saves known (in-the-index) files. `git stash -u` or `git stash -a` will, but I recommend that newbies to Git avoid those, and in general avoid `git stash` as well. – torek Dec 31 '20 at 07:50
-
I think op implies that that index.php is in the index just dirty. Also, I am going to leave my answer at this level which I think is most suitable for op, and upvote your comment. – Allan Wind Dec 31 '20 at 07:54
-
Yes, I already upvoted your answer myself, I mainly wanted to note that there's something deeper going on. :-) But you're right, I think index.php must be in the index at this point. – torek Dec 31 '20 at 07:56
So, if you have changes you haven't committed, they're going to be unaffected by switching branches. Of course, if switching branches is incompatible with your changes, git checkout will simply refuse to do it.
git add is a command for staging changes, which you will then commit. It does not record those changes into the history of the repository. It simply places them into a staging area (the index); git commit then uses the contents of that staging area to create commits.

- 478
- 3
- 12