Reputation: 9076
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!
Upvotes: 3
Views: 2467
Reputation: 134
When switching to a branch where some files are tracked and others are not, the tracked files will disappear, while the directory will remain with the untracked files inside. However, Git won't recognize the directory in this state.
Upvotes: 0
Reputation: 72266
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
.
Upvotes: 4
Reputation: 9076
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.
Upvotes: 0
Reputation: 2172
The changes i.e modified files, unless stashed or committed are transferred when you change your branch.
Upvotes: 0