19

Upon opening my ASP.NET Core project with an existing git repo in Visual Studio 2022, the git repo is not being loaded. However, opening a freshly cloned repo of this project from GitHub works fine. And opening other projects with git repos work fine. What could be the issue?

Raj Narayanan
  • 2,443
  • 4
  • 24
  • 43
  • 1
    Can you open that folder in a command prompt and do a git status and tell us what it says? – Dan Csharpster Apr 15 '22 at 13:37
  • @DanCsharpster It shows the standard output of a git status command. And it shows that I am on the develop branch. But, oddly, replacing the `.git` folder in the freshly cloned project with the `.git` folder in the existing project works fine in Visual Studio. – Raj Narayanan Apr 15 '22 at 13:46
  • So, It's something in the project itself that is causing this problem. – Raj Narayanan Apr 15 '22 at 14:14
  • And just to be sure, are you opening this project as a Visual Studio project, solution or a folder? – Dan Csharpster Apr 15 '22 at 14:44
  • That's weird about the .git folder. I would suggest using a diffing tool like WinMerge and comparing the two folders to look for differences. – Dan Csharpster Apr 15 '22 at 14:47
  • I also started running into this problem yesterday, so I did a repair on the install and now none of my local repos are recognized. Just says "One or more errors occured" – trker Apr 15 '22 at 18:00
  • @DanCsharpster I'm opening the `.sln` solution file. I'll use a diffing tool to see the differences. – Raj Narayanan Apr 15 '22 at 22:08
  • @trker: wow, "one or more errors occurred" is a really awful message. What errors? :) Seriously, maybe it's saving the actual errors to a log file or other hidden location? This does appear to be a VS issue though, since Git itself seems to be fine with these. – torek Apr 16 '22 at 00:52
  • 2
    Did you update Visual Studio to the latest version? I guess it is broken for me too. I updated VS 2022 and it also didnt recognise my repo. It works fine in 2019 and not in 2022. – Pankaj Nikam Apr 18 '22 at 02:14
  • @PankajNikam Yes, I did update it recently. This could be one source of the problem. – Raj Narayanan Apr 18 '22 at 11:14
  • https://github.blog/2022-04-12-git-security-vulnerability-announced/ Run git update-git-for-windows – larryjustin Apr 18 '22 at 16:09
  • Looks like a VS 2022 17.2.0 preview 3 problem. VS can't read any of my git repos. But the cmd line git says the repos are fine. – Ray Apr 18 '22 at 19:33
  • I ran the git windows 2.35.2 update and I still have the same problem VS 2022 even just had a new update (17.1.5) which did not help either. I have the same "open or more errors occurred" as well. Just fantastic. – Prethen Apr 19 '22 at 19:33

5 Answers5

21

Had the same issue after upgrading to latest version of VS2019 and had a look at the output from "Source Control - Git", there were messages of a fatal error because of no access and suggesting running the following:

git config --global --add safe.directory path/to/repo

After running it and restarting VS2019 it works OK.

patriml
  • 472
  • 3
  • 9
  • 5
    It's worth mentioning that the "path/to/repo" MUST to be specified using (Unix style) forward slashes and not (Windows style) backslashes. – Mort Jun 27 '22 at 10:54
  • 1
    This worked for me. It might be worth it to add that the command will need to be ran from a command prompt that was run as administrator. Also, I copy pasted the full command from the output > Source Control - Git window. The suggested command used `'` instead of `"` to wrap the directory. I got a git error: `error: wrong number of arguments, should be 2` I determined that this error was because my directory has a space in it. I replaced the single quote with a double quote, and it worked after that. – Trevor Vance Sep 22 '22 at 15:20
10

If you run VS 2022 as an Administrator, it should load everything as it should.

dpdragnev
  • 2,011
  • 2
  • 28
  • 55
  • I had granted options to launch VS as administrator. Then later on updated VS, and git stopped working. But your answer helped to figure out why - after update, I had to set option to launch VS as administrator again. Thanks. – kosist Jul 14 '22 at 09:58
  • Note that this is a *big* security risk. Visual Studio runs all sort of code, including your own and any third-party packages it might depend on... It's better to not give Visual Studio too many privileges. – Sjlver Dec 20 '22 at 15:14
  • @Sjlver, true, but in most cases you will have to run it as an admin anyway, if you want to debug your application. If you have any concerns about what VS is running in the background, you should be careful which packages you are adding to your solution. – dpdragnev Dec 20 '22 at 19:31
  • 1
    @dpdragnev you can debug most applications without being an admin. Either way: Other solutions for this question are better; users should simply own their project directories. – Sjlver Dec 21 '22 at 13:52
9

Had the same problem running VS2022 under Win10. Problem was that the ownership of the file system directory containing the solution and git data had changed to Administrator. After changing the ownership back to my normal windows user account everything worked fine again.

maze
  • 91
  • 2
6

In my case the project was cloned in console with Administrator rights. All worked good in console but Visual Studio was not read the .git folder.

Actually folders has enough rights for Authenticated Users group that was assigned for all child objects but Visual Studio still not see .git folder.

Only after change owner of project folder and all child objects to current account Visual Studio seen .git

Steps:

  1. Open Properties of project folder.
  2. On tab Security select button Advanced.
  3. Select tab Owner and button Change.
  4. In List select your account plus check "Replace owner on subcontainers and objects".
  5. Click Apply
Andrey Sv
  • 247
  • 3
  • 6
  • I had cloned my repository originally when running VS as administrator and when trying to run as a non admin, nothing loaded. These steps worked perfectly and VS 2022 can now see my git repo. – TaylorD Sep 19 '22 at 18:42
  • Worked for me. Same issues. Admin user. I forced the permissions to my windows user and set VS to ALWAYS run as admin. Seems to have done the trick. Really wish VS / Win 11 would report "user permission errors" so the user can identify permission issues more quickly. – Tomas Beblar Mar 08 '23 at 19:37
2

I ran into the same problem.

Visual Studio Code and Git bash correctly recognized the repo but not Visual Studio 2022.

I installed the latest version of Git in the system, and the visual studio recognized the git repo correctly.

Abhi
  • 21
  • 3