0

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,198
  • 12
  • 80
  • 161
  • 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

2

A Git branch is a pointer to a commit.
You did not commit your changes, hence 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 differences between the old and the new branch.

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>.

Read more about git checkout.

axiac
  • 62,164
  • 9
  • 85
  • 118
0

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

Rishabh Agarwal
  • 1,835
  • 1
  • 13
  • 31
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,198
  • 12
  • 80
  • 161