TFS/GIT in VS Cannot switch to master because there are uncommitted changes

36,612

Solution 1

Okay, I had the same issue myself as rwkiii - this is a bug in Visual studio and is not an easy one to fix. Here are the symptoms:

Visual Studio states that you cannot merge/switch braches/etc. because you have uncommitted changes - which is fine, except all your changes have been committed.

Cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches. See the Output window for details.

Here's a screenshot to be clearer.

If you look at the output window, it may appear empty but you have to change "Show output from" to "Source Control - Git". This will list the file that Visual Studio thinks has changes.

Make sure "Source Control - Git" is selected

Now, things that I tried that didn't fix it (so you don't have to):

  • Restarting Visual Studio
  • Restarting the machine
  • Switching Projects
  • Using external git tools (All of which said "no changes")
  • git reset
  • Making arbitrary changes to the file and committing that. Commits ok, VS still thinks there's more changes

In the end, I had to go and delete the file from the disk, then "undo" that change from the Team Explorer window:

Click "undo" to restore the file and your ability to switch branches

Not the most ideal solution, but it does seem to fix it.

Solution 2

I used the git command prompt to resolve the issue. I can't guarantee that it's the best way or the only way, but it worked for me in Visual Studio 2013.

Under Changes in the Team Explorer, choose Open Command Prompt.

In the command prompt type:

git status

It will list the files that are open for change.

Copy off those files (just in case), then you can then discard the changes in the working directory in the command prompt.

git checkout -- mysubdir\myfile.ext

Don't attempt to change branches in Visual Studio, it still won't work!

Instead at the command prompt switch to the branch in question.

git checkout branchname

When you go back to Visual Studio it will prompt you to reload the project. After that the branch will be updated correctly to the branch you chose from the command line.

hth

Solution 3

Hi this did the trick for me...

Try using the command prompt Team Explorer > Unsynced commit > Actions > Open Command Prompt

Do a git checkout branchName

Solution 4

I resolved a similar issue without having to delete a file or make new changes.

After committing changes on BranchA, merging with Master and pushing via Git bash, the next time I opened the project in VS and looked at Team Explorer, it showed that there were changes on the Master branch. No changes showed up when checking status via Git bash, but 10 files were listed in VS Output. Diffs on all files showed no changes. I tried a commit amend at this point, but it wouldn't let me amend the previous commit (see more about this below, since ultimately this will fix my issue).

Tried to switch to BranchA in VS and got the message that I can't switch due to uncommitted changes. Switch to BranchA via Git bash, and status shows no changes. Refresh VS Team Explorer so it is now on BranchA, shows 14 file changes, even more than Master, but again, diffs on all files showed no changes.

While on BranchA in Team Explorer, I clicked on Actions->Amend Previous Commit. This time it let it happen and all the changes were cleared. It then showed a pending commit, but checking status on Git bash showed nothing pending, so going to Home in Team Explorer and coming back, cleared the pending commit message.

Switched to Master in Team Explorer, no more changes showing there either. All is well.

Solution 5

1: Open git bash

2: Point to your git repository

3: check you branch status by using command "git status"

if you have any re usability of your changes for other branch then use "git stash save", (this will save your changes locally,you may face some conflict error, you can ignore for now) if you see any un-merged file in git status result and you want to keep the changes use "git add <file path>"

4: once your local changes are saved use command "git reset --merge"

this will reset your local branch to the original checkout, now you are ready to switch the branch you may see the conflict files in visual studio Team Explorer>changes, if you don't want to keep that file changes right click and select undo changes or use "git add <file path >" to add this file with other saved files.

5: Now your git branch is ready for switch, type "git checkout <branch path>"

6: to pop saved changes use "git stash pop", this will merge the saved changes in you current branch.

Share:
36,612
rwkiii
Author by

rwkiii

Updated on July 30, 2021

Comments

  • rwkiii
    rwkiii almost 3 years

    I've setup a GIT repository for a VS 2013 solution on visualstudio.com. The repository worked great while I was becoming accustomed to using TFS (in GIT mode).

    Then I decided to familiarize myself with Branches, so I created a branch off of the master. I've made quite a few changes in this branch. I've committed the changes over time and have performed a Sync to push my local commits to the visualstudio.com repository. This all works.

    The problem I am having is that I somehow lost the ability of switching back to the master branch. I cannot merge my newly created branch into the master. Every time I try to do anything that involves the master I get the following error in VS:

    Cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches. See the Output window for details.

    The Output window never contains any 'details'...

    What "uncommitted changes" does this message refer to? Since I cannot get into the master branch I have no way of committing any of its changes (nor am I sure that I want to?). And the current (only other) branch I am in has been committed and sync'ed.

    I'm really just learning TFS, GIT and source control. How do I safely recover from this problem?

  • rwkiii
    rwkiii over 9 years
    the branch I am working in is important and so I don't think I want to merge/pull any changes from the master branch. To be honest, I don't see how there could be any changes in the master branch since I performed a commit and sync before creating the new branch. This problem has been going on for weeks... Do you have a way of knowing which branch contains the uncommitted changes? Is it in my local GIT repository or on visualstudio.com? Are the uncommitted changes in the master branch (that I can't get to) or the new branch I have been working in? :S
  • Asser
    Asser over 9 years
    Uncommited changes are always local and on the branch you are currently on. If Visual Studio is acting up and not showing pending changes my recommendation would be to install git from i.e. git-scm.com/downloads and then use the Git Gui tool to see any pending changes.
  • David Masters
    David Masters about 9 years
    +1. I had no changed files (in fact I had only just cloned the repro) yet VS wouldn't let me switch because of this error. In the end I just ran the command git checkout <branch> and this worked fine. Clearly a bug in the VS plugin.
  • Tigran
    Tigran almost 9 years
    That was quick and easy. Thanks. I'm pretty sure the problem started when I renamed a file.
  • Rich Ward
    Rich Ward over 8 years
    I have to concur. I was looking all over without luck. I am using VS 2013 and I was stuck on a build - could not merge back to master. I also wasn't seeing the output - so the tip about changing to, "Source Control - Git" was very useful. One note: After deleting the file from disk your file may not appear in Included Changes. Mine showed up in Excluded Changes.
  • Jay
    Jay over 8 years
    This seemed to do it for me, I had no commits also, I did a git checkout <branchname> and it worked after that
  • Piotr Kula
    Piotr Kula about 8 years
    This didnt work for me ;( It was a fresh install of VS2013. Did a fresh clone from master and it did a project "convert" and added a log file. I think that threw it off track even though output was showing a random file? Anyway, I did a manul git checkout and that fixed it. I can now change freely without problems.
  • Piotr Kula
    Piotr Kula about 8 years
    Just running the chekcout manually fixed it for me. I did a fresh VS2013 install, Git Windows install. Cloned and it did a project convert, inserting a log file. It got stuck there, i couldnt change from master. I did a manual checkout and all the branches work now. Phew! +1
  • Dhanuka777
    Dhanuka777 about 8 years
    I was also thinking about doing this.... good one! After spending so much time to solve this I feel like it's time to get away from Gui tools for Git
  • hfrmobile
    hfrmobile about 8 years
    Worked for me with VS2015 Community Edition. Any ideas when this bug occurs? It is really annoying when this occurs for many files in different directories when doing FI (forward integration) and RI (reverse integration) .... missing the days when we used SVN (Subversion) or TFS Source control ... never had such issues with SVN and even TFS source control seems to be more stable .... This and other issues let me hating Git ...
  • Sentinel
    Sentinel about 8 years
    Basically works, found the right files in the Output window after changing the window's view, but I found the problematic files in the "Untracked files list". I right clicked them and pressed "Ignore...." This solved the problem.
  • Kushan
    Kushan about 8 years
    @Sentinel I would suggest updating your .gitignore file to use the one listed on github: github.com/github/gitignore/blob/master/VisualStudio.gitigno‌​re This tends to solve issues like what you've just described and is a good start for 99% of Visual Studio projects.
  • Jive Dadson
    Jive Dadson over 7 years
    Hello from 2016. These days, it is not always possible to talk VS into undoing a change. Sometimes it gets into a state where you can click on "Undo changes" and "Yes, dammit, I do mean it. That's why I clicked the other button," as many times as you please, but to no effect. One workaround is to commit the (fictitious) changes, then revert the committal. In the obligatory description of the committal, type something like "Appease GIT."
  • Nadav Ofir
    Nadav Ofir over 7 years
    Worked for me as well (visual studio claimed I had uncommitted changes when I never made any changes at all!)
  • mihkov
    mihkov about 7 years
    The problem with git not bothers me rather than VS 2015 won't showing the changed file in output window as the message says. This is defensively a bug in VS, Is it reported or where I can report this issue? Once I have wrote them, but no response.
  • Jason
    Jason almost 7 years
    vstudio: right click undo on proj in solution explorer
  • Dib
    Dib almost 7 years
    @Jason - but you have to do that each time which gets frustrating. My solution is a one time fix
  • PTansey
    PTansey almost 7 years
    I did issued a "git status" and it said my branch was up to date, nothing to commit, which should have been true. What's so strange is the next time I tried to merge it worked and I don't believe anything else changed (other than my "show output from" in my Output window) I checked the documentation and it doesn't appear git status changes anything so I'm not sure how to explain this sudden successful merge
  • TestyTest
    TestyTest over 6 years
    Open a command prompt, do 'git status' and in VS perform a stage of pending files
  • RRRSR
    RRRSR almost 6 years
    I had same situation in vs2013: was in my only branch off of master and had committed all and synched all and got the same error when I tried to switch to Master from the current branch. Git status showed nothing to commit. Did checkout of master in git and all was fine. Went back into VS Team Explorer and the branch was now master. Then did a checkout of the other branch (in git) and VS 2013 showed the branch correctly. All is now copacetic. Agree: big MS bug; criminal negligence to not fix it
  • Ashish Kamble
    Ashish Kamble over 4 years
    Ohhh, My all changes are gone Nowww
  • rbansal
    rbansal over 4 years
    Yes reset --hard will delete your changes
  • Ray Hogan
    Ray Hogan over 3 years
    Thanks for this! Solution still relevant in 2020 using VS 2019.