2

I have a very strange problem in my Visual Studio 2017 (latest version is installed) and my GIT repositorys.

Since a few weeks I am able to switch to another branch without the need of commit my code changes.

Example:

  • I am working on branch-1 an a add a file
  • now am able to change to branch-2
  • in branch-2, the new file is also available

That should not work! It makes branches just useless! How can I fix this strange behaviour?

Thanks! Stefan

EDIT: Maybe my example was not the best. I am sure all files are tracked. I try another example:

  • I create a new branch "new_feature"
  • in this branch I make changes to the existing feature.aspx file
  • the feature.aspx is listet in the "uncommited changes" area correctly
  • then I change back to the master branch
  • I expact to get back the unchanged feature.aspx (as it has worked for serveral month before)
  • but the featue.aspx includes my changes and is listed under "uncommited changes" of master branch

In each branch - the code files are the same.

UPDATE:

I found out the following: If I commit the changes in the feature.aspx in new_feature branch and switch back to the master branch - then I get back the unchange feature.aspx as I would expact it. But this is a potential error source! So it seems that I just want to get back the restriction, that I can not change the branch, if there are uncommited changes in the current branch. Where can I find the this setting? Is it a feature of VisualStudio or is it a feature of GIT?

Stefan Preuß
  • 31
  • 1
  • 4
  • 2
    Possible duplicate of [Why git keeps showing my changes when I switch branches (modified,added, deleted files) no matter if I run git add or not?](https://stackoverflow.com/questions/5531362/why-git-keeps-showing-my-changes-when-i-switch-branches-modified-added-deleted) – Matt Mar 18 '19 at 15:41
  • 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 Mar 18 '19 at 18:15
  • @Stefan did you figure out what the problem is? I understand completely what the issue was because I'm seeing it at the moment - The file I've updated is definitely being tracked by GIT as we've made multiple changes to the files in question – Dan Harris Jul 07 '21 at 08:48

2 Answers2

0

If the file is un-tracked, then Git doesn't have any record of the difference in content for that file between branches. If that file is supposed to be part of your repository on that branch, you should commit the file, or at least stage it for commit and use git stash. If stashed, you can git pop the file back into your index when you come back to branch-1.

Josh Gust
  • 4,102
  • 25
  • 41
  • Thanks for your anwser! It seems that I used a feature of GIT/VisualStudio which is now no longer available or it is disabled. Please have a look on my EDIT and my UPDATE part of my answer. Maybe you would better understand what I mean. – Stefan Preuß Mar 20 '19 at 09:30
0

While I agree with Josh Gust's answer, here is a more Visual Studio-centric approach:

It seems that the file you're adding is outside the folders that Git can track for your solution. To determine what that is, you need to locate the .git folder--it's a hidden folder, so make sure you're able to view hidden folders in File Explorer. Usually you can right-click your solution in Solution Explorer and select Open Folder in File Explorer from the context menu. Once there, you should see the .git folder if your solution is under Git source control. Take a look at the folder/file path to the .git folder, and that's the Git root. Armed with that information, you need to ensure that any file you add gets added to either that root folder or to one of its sub-folders. Based on what you describe, you're adding a file that's above--and thus outside of--your solution's Git root, and in that case, that newly added file cannot be tracked.

Jazimov
  • 12,626
  • 9
  • 52
  • 59
  • Thanks for your answer! Maybe my example was not the best. I am sure all files are tracked. I try another example: - I create a new branch "new_feature" - in this branch I make changes to the existing feature.aspx file - the feature.aspx is listet in the "uncommited changes" area correctly - then I change back to the master branch - I expact to get back the unchanged feature.aspx - but the featue.aspx includes my changes and is listed under "uncommited changes" of master branch – Stefan Preuß Mar 19 '19 at 10:24
  • It seems that I used a feature of GIT/VisualStudio which is now no longer available or it is disabled. Please have a look on my EDIT and my UPDATE part of my answer. Maybe you would better understand what I mean. – Stefan Preuß Mar 20 '19 at 09:30