8

Please help me, my VSCode and git were working perfectly but all of a sudden, it stopped recognizing my .git folder.

I open the VSCode terminal in the same path my .git is, but it did not recognize the git, and cannot send any git command.

This is crazy, when I create an empty folder and clone my repo, it worked perfectly BUT ONLY 1 MINUTE.
It cloned, it showed the files in the root explorer of the VSCode BUT then it magically vanishes and OMG I didn't do a single thing, in VSCode second image you can see that it says "the folder currently open doesn't have a git repo" but THAT'S A LIE, 1 minute earlier, it showed me my repo and let me do commits (well, I didn't do any of them because it vanished so quickly)

Please, help me, I don't know what to do, I'm using VSCode "STABLE" BUILD, Git downloaded from https://git-scm.com/.

Even when I open git bash in my folder, git is not recognized!! How is that possible? It worked a minute ago in the VSCode terminal!!

vscode pic

vscode pic 2

root

.git root folder

git bash

torek
  • 448,244
  • 59
  • 642
  • 775
rocksteady.24
  • 91
  • 1
  • 1
  • 4

8 Answers8

10

You need to add the repo directory as a safe directory with the recommended command:

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

you can also trust any directory (But it is not really recommended)

git config --global --add safe.directory *

if you face an error like this:

error "fatal: bad config file line 1 in .git/config" 

please look at 27073427 answer

Hadi Masoumi
  • 1,153
  • 9
  • 13
  • For those wondering why it happens, one of the reasons is when you format your computer, keeping a backup. Repo is owned by ... but the current user is ... – ferreiradev Nov 20 '22 at 17:21
  • Thank you, this worked for me. What's weird is that the repo worked fine for over a year, and then one day just stopped working. Local drive, didn't reformat, git status was normal, etc. – Formica Jan 26 '23 at 21:24
  • Does anyone know or have any information as to why this would suddenly occur on a repository that has worked fine for months? Similar to what @Formica mentions? – bmitc May 02 '23 at 13:09
3

A recent fix for a vulnerability CVE-2022-24765 caused similar issue for me when opening project over a samba share. The fix requires you to add the project directory to a "safe" list:

git config --global --add safe.directory '%(prefix)///server/path/to/project'

Also mentioned here

Krister
  • 31
  • 5
3

I hade the same issue. Running VSCode as administrator fixed the problem for me.

Ghaiath
  • 33
  • 3
1

Double-check your Windows 10 Defender setting. The latest W10 21H1 edition comes with a ransomware protection which can prevent processes to access certain folders and/or quarantine files/folders.

And make sure to not create a repository in a synchronized folder like OneDrive: The Git repository state will get desynchronized at some point for sure.

To test both those possible causes, try and create or clone a local repository in a file outside %USERPROFILE%/OneDrive, with a simple path like C:\myNewRepo.
See if a VSCode still has the same issues then.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

In vscode settings there is an scan directory option under:

Git: Scan Repositories List of paths to search for git repositories in.

Add Main directory or drive letter and it will automatically find.

ilkerdev
  • 301
  • 3
  • 4
0

Adding this for whomever might need it. The accepted answer didn't work for me.

I had to re-enable some Java extensions (Debugger, Extensions Pack, Language Support, Maven, Project Manager) for VSCode that I previously disabled. I am not working with Java but, in my case, it is probably a company setting.

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Lorenzo
  • 64
  • 8
0

Sometime the reason is that git client is not installed in OS or VS Code is not able to find it.

Additionally, when you open VS Code in folder which is child one of folder holding git folder (.git) then VS Code will not notify about missing git client.

Just install git client (git.exe) or verify both and compare:

  • git path in VS Code's settings (just search for 'git path' in the settings)
  • the real path of git in your OS
Bronek
  • 10,722
  • 2
  • 45
  • 46
0

I was opening my cloned git projects in my WSL which VS Code would not recognize. Eventually, VS Code recognized I was in WSL and asked me if I wanted to open the folder in WSL. When I clicked yes, it fixed it.

In VS Code there is a green section at the bottom, left hand corner where there are two arrows enter image description here that are stacked up on each other. Click it and it will allow you to use WSL if you have it set up.

enter image description here

Clicking the green stacked arrow menu button will bring up a menu and in the menu, if you have WSL in VS Code, you should be able to select "New WSL Window."

enter image description here

Here is more information on WSL with VS Code if you need to set it up and install it: https://code.visualstudio.com/docs/remote/wsl#_getting-started

Carter
  • 21
  • 1