1

After years of working as a sole developer with one git master branch, I am investigating using additional branches. My plan is to keep master as a production-ready branch, do development in a develop branch, and create feature branches off develop.

After much reading, I have created a branch (from my master branch), called develop:

git checkout -b develop

I then created a feature branch off develop:

git checkout -b my-feature

I then modified a file in the my-feature branch and switched back to develop as follows:

git checkout develop

The problem is that when I view the file I changed in the feature branch, the change is still visible. Why is that so? I am on the develop branch!

DatsunBing
  • 8,684
  • 17
  • 87
  • 172
  • Have you added the file you changed? Unless it's tracked by git, the file won't be affected by the branch switching – OliverRadini Sep 02 '18 at 08:58
  • Possible duplicate of [Git allows for branch change with unstaged changes](https://stackoverflow.com/questions/8526279/git-allows-for-branch-change-with-unstaged-changes) – jonrsharpe Sep 02 '18 at 09:08
  • 1
    You are using git wrong. The change you made in `my-feature` needs to be committed (`git commit`) first before you move back to `develop` branch. Here's an excellent set of tutorials https://www.youtube.com/playlist?list=PL-osiE80TeTuRUfjRe54Eea17-YfnOOAx – GMaster Sep 02 '18 at 09:09
  • Possible duplicate of [Modified files in a git branch are spilling over into another branch](https://stackoverflow.com/questions/246275/modified-files-in-a-git-branch-are-spilling-over-into-another-branch) – phd Sep 02 '18 at 09:12

3 Answers3

4

A Git branch is a pointer to a commit.
You did not commit your changes, therefore they are not in any branch but only in your working tree.

When it checks a different branch out, Git preserves the changes present in the working tree, unless they conflict with the changes introduced by the branch to be checked out. If they conflict then Git refuses to switch the branch (unless it is called with one of --merge option or --conflict option, but this is not in the scope of this question.)

The documentation of git checkout explicitly mentions:

git checkout <branch>

[...]

Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

[...]

When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context.

Read more about git checkout.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • "conflict with the differences between the old and the new branch" How exactly to interpret this sentence? – hrdom Jul 18 '23 at 08:53
  • I guess it needs to be rephrased... – axiac Jul 18 '23 at 09:07
  • I have read the detailed explanation of this question and I understand. https://stackoverflow.com/questions/8526279/git-allows-for-branch-change-with-unstaged-changes – hrdom Jul 18 '23 at 09:08
0

The changes i.e modified files, unless stashed or committed are transferred when you change your branch.

Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
0

Thanks for the helpful suggestions. I did actually commit to the feature branch before switching to develop. I should have mentioned that in the original post.

The problem was that PhpStorm was not refreshing properly. I hit the synchronize button, and now changes seem to be detected correctly when I move between branches.

DatsunBing
  • 8,684
  • 17
  • 87
  • 172