'Exception of type 'Microsoft.TeamFoundation.Git.Contracts.GitCheckoutConflictException' was thrown

While trying to checkout to a new branch in Visual Studio 2019, I'm getting the below error: enter image description here

I tried to do the same from GitBash cmd using the command git checkout branchname but here also, I'm getting an error:

error: The following untracked working tree files would be overwritten by checkout:
//list of file names below
...
...
...
Aborting

I tried restarting VS also but it was of no help. Can you please assist?



Solution 1:[1]

I had this same error and solved it the following way, note I am calling my branch as myBranchName... Within visual studio in the git-manage branches screen, it just shows that error over and over. But, I navigated to the folder in windows, right-mouse context menu and chose 'git bash here' to open bash (which you can install along with GIT for windows in the same installer), in that folder. Then I typed

git checkout -b myBranchName

and pressed enter. This worked with no error. Then I returned to visual studio and it showed myBranchName in the bottom right of the screen.

Note: Also this all assumes your "new branch name" is an existing branch, in my case it was an existing branch that existed already remotely and locally when I got that error. If this is different from your question, then this may/may not help.

Solution 2:[2]

I was also facing the same issue and the below steps worked for me

  1. Navigated to \.git\refs\remotes folder in my local system
  2. Deleted the branch that was causing the issue
  3. Now i checked out the branch using git bash using git checkout <branchname>

Solution 3:[3]

Here is the way I fixed this:

Our Cause: We reset our repository's master branch, but the master branch only had the initial git file. The rest of our work was delegated to a backup branch that we were going to merge via a pull request. After resetting master and moving the data to the new branch, whenever we tried to pull the data from the remote/origin into visual studio from the backup branch, it caused this exception. We tried removing the solution and redownloading, and other techniques, but each one failed.

First Solution: After many attempts, I recreated a .vs solution in Master and pasted the .gitignore, Jenkins, and Nuget files from the backup. For some reason after that local commit, I was able to pull the backup commit without the exception.

Second Solution: Deciding that maybe it had something to do with either the .gitignore,Jenkins,Nuget files being out of sync, I decided to try again. I deleted the repository from my computer, again, and pulled the repository, again. I copied and pasted the .gitignore, Jenkins, and Nuget files into the master branch, and locally committed. Following that, I was once again able to pull the Backup branch without visual studio throwing the exception. So, for us, it had something to do with either one of those files, or for some reason we just needed to make a local commit on Master before it would allow us to pull any additional branches.

Solution 4:[4]

Solution 5:[5]

None of the above answers helped me. In the end I simply re-cloned the repo to a different folder and it started working again.

If deleting the suo file does nothing and trying to checkout the branch just has it create a new local branch instead, just re-clone and save yourself time.

Solution 6:[6]

I just ran into this issue (VS 2022). My situation sounds very similar to Patrick's, so I'll share how I fixed it in case it helps anyone:

In my case, the branch switch looked successful at first (in reality it hadn't completed), and then I started receiving said exception immediately afterwards. According to the Git Changes toolbar, I was trying to add a file that existed in the branch I was coming from that wasn't present yet in the branch I was going to. (Note there were several files that existed in one branch but not the other. But for some reason, this particular one had tripped things up.)

All I had to do was this:

  1. Take the file in question out of the directory
  2. From the shell window, switch back to the first branch, then switch to the second branch again. After this, everything merged fine for me and Visual Studio stopped throwing that exception.

(For those that don't know how to do any of this, you can get to the command line from Tools -> Command Line. Then enter git switch branch_name where "branch_name" is the name of the branch you're switching from, then repeat the command with the name of the branch you're switching to. If that goes well, you can then merge the first branch into the second by typing git merge branch_name where "branch_name" is the branch you switched from.

Solution 7:[7]

If you are having this problem you need to think high level. I got this problem when I was trying to switch to a different git branch and it was really fussing and going back to the original git branch when it failed. If you don't have a lot of changes in your local repo I have a simple solution for you.

Go to your repos directory and delete the folder for the project that is giving you this ...GitCheckout... problem. Obviously this solution will lose any changes in your local repo so only do this if you've just checked in. It may be that since you only get this message trying to switch to a different branch, you haven't made a lot of changes there yet. Nothing wrong with checking changes in for the branch you are stuck on and then trying what I suggest. Then clone that repository again to your local repo and go from there to the git branch you want. To do that in VS 2019 pick the "Clone a Repository" and pick the high level project you want to clone. There's nothing wrong with your central git repository, this message is just some syncing issue in Visual Studio between the git resource and your local repo that only 5 people who work for Microsoft really understand.

Solution 8:[8]

Check to see if your branch was renamed.

Solution 9:[9]

Just in case: For me the solution was re-open the solution as administrator and make sure that you do not have pending files to protect if you are trying to move to another branch

No error message anymore

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris
Solution 2 Sud
Solution 3 Patrick Knott
Solution 4 AEhrlich
Solution 5 Jeremy Styers
Solution 6 Dan
Solution 7
Solution 8 Kelly R
Solution 9 Hugo Zeferino de Jesus