Visual Studio 2015 Git error message "Cannot pull/switch because there are uncommitted changes"

Visual Studio-2015Azure DevopsGit PullGit CheckoutGit Revert

Visual Studio-2015 Problem Overview


I am having difficulty in doing a pull from origin. I keep getting:

> "Cannot pull because there are uncommitted changes. Commit or undo your changes before pulling again. See the Output window for details."

This also applies to switching branches. I get a similar sort of message, but this does not always happen.

I am using Visual Studio 2015 Update 1 and Visual Studio Team Services Git. On my machine I have a local master branch, and development branches. Every time I switch to master and then I do a pull I get the error message. I have resorted to doing a stash and drop stash (command line) and sometimes I use TortoiseGit to do the pull and it works.

What is strange is even if I try to revert (on the uncommitted files) using TortoiseGit it shows that it was reverted successfully (I have already tried Visual Studio undo, nothing happens). Trying to pull again, it is still the same problem. The uncommitted files will be there and sometimes when I do a git status it says there is nothing to commit.

Just a note: This can happen even after switching from a branch onto master. In this case there is no way there can be uncommitted changes, because I would not have been able to switch in the first place.

I am still new to Git, but I would like to know if there is a better way of solving this as I would like to use one environment instead of using switching between different environments for each task; it's easier for me to just do everything from Visual Studio. I have already read up on:

https://stackoverflow.com/questions/26885057/tfs-git-in-vs-cannot-switch-to-master-because-there-are-uncommitted-changes

UPDATE

It seems like this problem has to do with line endings.

By doing a git diff -R you can see that a line ending has been added, "^M", and it is different. Removing the * text=auto in gitattributes (then check for changes) and putting it back on again so that the gitattributes does not signal a change of itself that needs to be committed seems to help, there will not be any changes.

Visual Studio-2015 Solutions


Solution 1 - Visual Studio-2015

For me I didn't have any uncommitted changes or any untracked files, and Visual Studio 2015 still presented the warning.

  1. Close the solution in Visual Studio, exit Visual Studio.
  2. Open Git Bash (or your favorite Git UI)
  3. Navigate to your repository (or open the repository with the Git UI)
  4. git pull (or perform pull on the Git UI)
  5. Merge happens (hopefully no conflicts, as in my case), vi opens (or the default merge resolution tool)
  6. :wq then press ENTER in vi (or calm handle the merge tool which popped up optionally) and hopefully this resolves it just like for me.
  7. Start Visual Studio 2015, open the project

I added a safer step-by-step by instructing closing and opening the solution and Visual Studio. This may be over cautious, and maybe a reload would be enough. This symptom could be a bug of the Visual Studio Git integration parts, and maybe it'll be resolved in the future.

Solution 2 - Visual Studio-2015

Type git status into a command line opened at that directory. If there is red and/or green text, you have changed some stuff and not added and committed. Either revert the files (by doing git checkout -- <file>), or add and commit (by doing git add --all then git commit -m "commit message"). You can then check out branches or whatever else you want to do.

Solution 3 - Visual Studio-2015

Try with these commands by going to the working directory of the project in the command prompt.

git add -A
git commit -m "your message"
git fetch origin master
git pull origin master
git push origin master //To push to the Git system

Solution 4 - Visual Studio-2015

I had this issue in Visual Studio 2017 build 15.5 and what fixed it for me was going into Team Explorer SettingsGlobal Settings and setting "Prune remote branches during fetch" and "Rebase local branch when pulling" to True.

Team Explorer Git Settings

http://www.codewrecks.com/blog/index.php/2017/12/23/configure-visual-studio-2017-15-5-for-pull-rebase/

Also MS has the Instructions here: https://docs.microsoft.com/en-us/azure/devops/repos/git/git-config?tabs=visual-studio&view=vsts#prune-remote-branches-during-fetch The facepalm moment is when you realize that "We recommend setting this option to True at the global level." is not the default setting on those items.

Another instance of this issue occurs in the case where you have a separate build server and the solution gets frequent NuGet updates on the compilers. The First person to update NuGet Packages and sync the repo will have no issues, but if someone else attempts the update locally, before performing a sync, this message will pop up and VS won't allow you to pull or push code. To prevent this issue everyone should get into the habit of syncing before doing any updates locally. Once encountered, The best way we have found to resolve it is to stash any uncommitted code you need to keep (https://marketplace.visualstudio.com/items?itemName=vii.GitStashExtension), then reset your repository to the current head.

Solution 5 - Visual Studio-2015

This happens sometimes even with me. If you are using Visual Studio, there is an easy way to make your way clear.

For Visual Studio 2013 and above, follow the following instructions as this worked for me:

  • Go to menu ToolsNuGet Package ManagerPackage Manager Console.
  • Type git reset and hit Enter

That's it. Git will be reset and then you will be able to pull your request easily.

VS2015: Tools > Nuget Package Manager > Package manager console. Worked like a charm. Thanks.

Solution 6 - Visual Studio-2015

This issue usually occurs when there is file/change that hasn't been committed locally, but it is included in the pull action.

For example, you add a new file "test.txt" from your local Git repository folder directly (outside of Visual Studio). This file may be listed in the "Untracked Files" option, so when you commit changes, this file isn't been tracked and committed. And then another person also create a "test.txt" with the same path and commit&push it. Now, when you perform a pull action, "test.txt" file is included in the pull action and you will get the error message which indicates that there are uncommitted changes. So you need to check if you have files/changes that are not been committed and commit them before pull.

Solution 7 - Visual Studio-2015

This worked for me. I just went to the project directory and checked if something is there

git status

Then I performed a pull by using

git pull

Then I restarted Visual Studio 2015 and pulled the branch that was causing this error. This time with no issue. It seems like it was a bug in Visual Studio 2015.It only occurs when you undo your changes before pulling new changes. This same problem does not seem to be existing in Visual Studio 2017.

Solution 8 - Visual Studio-2015

I had this problem, too. An easy solution was that I selected Commit on the solution to see the uncommitted files (or easily use Team ExplorerChanges).

Then I undo each file using right-click. Before, I tested it by Undo on the whole solution, but it didn't work properly. Then again, I used Team ExplorerSyncPull, and it worked.

Solution 9 - Visual Studio-2015

> Can not pull because there are uncommitted changes. Commit, stash, or undo your changes before retrying. See the Output window for details.

There is a simple solution for fixing this error:

First commit your changes stash or undo. Then pull the code. e.g.

git pull

Then it should work fine.

Solution 10 - Visual Studio-2015

Here are the steps that I've followed:

  1. Refer to the path of the uncommitted file in the output window
  2. Navigate to that path and delete that file from the folder
  3. Undo your changes from Team Explorer in Visual Studio to add it again

Solution 11 - Visual Studio-2015

For me, the "Update" in the question was the answer. I added a .gitattributes file at the root of my repository and included only the following, so that line endings would only be normalized in cases where the file is text.

*.txt text
*.html text
*.css text
*.js text
# These files are text and should be normalized (Convert crlf => lf)
*.gitattributes text
.gitignore text
*.md texttesting 

Solution 12 - Visual Studio-2015

For me, this issue was caused by having two files, "Web.Config" and "web.config". I guess this is ok in Linux/Unix but Windows can only store one of them locally. I detected this in azure devops exploring the files. I deleted one of them and problem was solved. I guess this problem could be caused by any file.

Solution 13 - Visual Studio-2015

In Visual Studio open Output Window and switch Show output from to Source Control - git, git will let you know about what stops it from pulling. One of the most common causes can be of course something like this:

The pull would overwrite your local changes to the following 44 files:
    <Here you can probably see a list of 44 files>

It simply means that 44 files have been added to the remote repository which are not part of the local repository. Open Git Bash and run this command:

git add *

This may solve the problem or end up to a an error like this:

$ git add *
The following paths are ignored by one of your .gitignore files:
TestResults
Use -f if you really want to add them.

If you are sure about adding them to the local repro, just add them using git add * -f or remove mistakenly added files from remote.

Hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkwiriView Question on Stackoverflow
Solution 1 - Visual Studio-2015Csaba TothView Answer on Stackoverflow
Solution 2 - Visual Studio-2015TechnicalTophatView Answer on Stackoverflow
Solution 3 - Visual Studio-2015DhruvView Answer on Stackoverflow
Solution 4 - Visual Studio-2015user1431356View Answer on Stackoverflow
Solution 5 - Visual Studio-2015Yagnesh KhamarView Answer on Stackoverflow
Solution 6 - Visual Studio-2015Eddie Chen - MSFTView Answer on Stackoverflow
Solution 7 - Visual Studio-2015Rachit ChauhanView Answer on Stackoverflow
Solution 8 - Visual Studio-2015ElnazView Answer on Stackoverflow
Solution 9 - Visual Studio-2015Aditya RajView Answer on Stackoverflow
Solution 10 - Visual Studio-2015CSharpView Answer on Stackoverflow
Solution 11 - Visual Studio-2015JoshView Answer on Stackoverflow
Solution 12 - Visual Studio-2015Emil HelldinView Answer on Stackoverflow
Solution 13 - Visual Studio-2015BabakView Answer on Stackoverflow