在 Windows 中取消初始化 git 回购的命令

command to uninitialize a git repo in Windows

What specific syntax needs to be used for a command that works in Windows to uninitialize a git repo?

在 Windows 服务器中,当 Python 程序尝试 运行 shutil.rmtree() 目录时,我们遇到以下错误 包含一个 git 回购协议。正如您在下面看到的,该错误表明对 .git 子目录中的文件的访问是 被 shutil.rmtree() 命令阻止。

我们已阅读 this other posting,其中包含诸如在 Linux 中使用 rm -rf 或手动删除 Windows 中的 .git 文件夹等建议。我们还阅读了表明 shutil.rmtree() 不能以非管理员用户身份强制销毁。

However, the directory was created by the same user running a git clone command, so we imagine there must be some git command that will purge all the files in .git.

鉴于我们的用户可以 git clone,我们想象我们的用户可以 git uninit。那么我们需要使用什么命令来近似 git uninit 并有效地删除 .git 文件夹及其在 Windows 中的所有内容而不抛出以下错误?

Traceback (most recent call last):
  File "C:\path\to\myapp\setup.py", line 466, in undoConfigure
    shutil.rmtree(config_path)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 739, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 612, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 612, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 612, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 617, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 615, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'C:\path\to\callingDir\.git\objects\pack\pack-71e7a693d5aeef00d1db9bd066122dcd1a96c500.idx'

您基本上是在问“如何删除 Windows 中的文件夹及其内容”?所以 rmdir /s the\offending\folder?假设 git 存储库处于您想要保留的状态(即当前未检出某些旧版本),并且您确定要将其删除(无需用户确认),这有效:

rmdir /s /q .git

/s 确保内容也被删除。 /q 确保操作安静(假设 'y' 进行确认)。

如果您使用的是 PowerShell 而不是命令提示符,您可以使用:

Remove-Item ".git" -Force -Recurse