Git添加错误: 无法打开目录: 权限被拒绝。

5

我使用的是Windows 10。当在Git Bash中输入“git add .” 命令时,我一直收到以下信息:

warning: could not open directory 'AppData/Local/Application Data/': Permission denied
warning: could not open directory 'AppData/Local/ElevatedDiagnostics/': Permission denied
warning: could not open directory 'AppData/Local/History/': Permission denied
warning: could not open directory 'AppData/Local/Microsoft/Windows/INetCache/Content.IE5/': Permission denied
warning: could not open directory 'AppData/Local/Microsoft/Windows/Temporary Internet Files/': Permission denied
warning: could not open directory 'AppData/Local/Temporary Internet Files/': Permission denied
warning: could not open directory 'Application Data/': Permission denied
warning: could not open directory 'Cookies/': Permission denied
warning: could not open directory 'Documents/My Music/': Permission denied
warning: could not open directory 'Documents/My Pictures/': Permission denied
warning: could not open directory 'Documents/My Videos/': Permission denied
warning: could not open directory 'Local Settings/': Permission denied
warning: could not open directory 'My Documents/': Permission denied
warning: could not open directory 'NetHood/': Permission denied
warning: could not open directory 'PrintHood/': Permission denied
warning: could not open directory 'Recent/': Permission denied
warning: could not open directory 'SendTo/': Permission denied
warning: could not open directory 'Start Menu/': Permission denied
warning: could not open directory 'Templates/': Permission denied

2
你当前的工作目录是什么?C:\Users\<username>?恐怕你长时间以来一直在错误的路径上使用Git命令... - Geno Chen
@iBug 同意。这些文件夹很可能是 Windows 下的主目录。 - Geno Chen
@GenoChen 非常感谢您的回复 /c/Users/Mateo - Mateo Chiyangi
@GenoChen 我应该使用哪个路径?我是Git的新手。 - Mateo Chiyangi
@MateoChiyangi,你的代码项目路径。你可能有许多代码项目在许多不同的文件夹中。 - Geno Chen
显示剩余3条评论
4个回答

12

如果有其他人在寻找此错误时,并且他们已经确定正确的路径,那么此问题可能是由于工作目录中未保存的文件引起的 - 请确保所有文件都已保存,并关闭任何已打开的IDE。


2
请尝试确保所有文件已保存,并关闭任何打开的集成开发环境。- 这很有帮助。我在我的文本比较工具中打开了一个文件。谢谢! - Kislay Kishore

2

您正在一个意外的目录上运行Git子命令。

大多数Git子命令都是依赖路径的。在大多数情况下,这些子命令将当前工作目录视为Git根目录,并在Git根目录内部执行Git操作。

对于常规用法,我们将代码项目的根目录视为Git根目录。多个代码项目会导致多个文件夹和多个相应的Git根目录。使用当前工作目录来指定它们。

默认的当前工作目录称为Home目录,它可以是*nix中的~或大多数情况下的/home/<user>/root,或者在Windows 7之后是非管理员用户的C:\Users\<user>C:\Windows\System32,在Windows XP或更早版本中是C:\Documents and Settings\<user>

如果您没有指定当前工作目录(使用cd命令),所有的Git操作都会在您的Home目录下进行。在某些情况下,这可能会有害。小的灾难就像您遇到的问题一样,大的灾难可能会导致您意识到后意外将私人文件上传到GitHub或类似的地方。


非常感谢您的详细回复。我如何确保我在正确的目录中? - Mateo Chiyangi

0

我解决了同样的问题,只需要在管理员权限下运行终端以获取根访问权限。


1
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

-1
  1. 首先进入该路径并删除 .git 仓库。(在您的文件管理器中)
  2. 在所需文件夹中打开 Git Bash(这意味着通过右键单击文件夹)
  3. 通过输入命令 ($ git init) 创建 .git 仓库。
  4. 这样会创建一个 .git 文件(.git 文件将在您所需的文件夹中创建)。
  5. 现在检查 git 状态。 万岁!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接