git Git没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40178828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:22:25  来源:igfitidea点击:

Git no such file or directory

git

提问by Daniel Novoa

I'm building an application and as always I'm using Git for version control.

我正在构建一个应用程序,并且一如既往地使用 Git 进行版本控制。

Below are the inputs and outputs, hope you can help me:

以下是输入和输出,希望你能帮助我:

# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        app/CodeBehind/Con.php

# git add app/CodeBehind/Con.php
fatal: unable to stat 'app/CodeBehind/Con.php': No such file or directory

To me that doesn't make any sense and I haven't found a solution yet, I read in two post that I should remove the file from Git, something like git rm "[fileName]"but it doesn't find a match with the file name Con.php.

对我来说这没有任何意义,我还没有找到解决方案,我在两篇文章中读到我应该从 Git 中删除该文件,例如git rm "[fileName]"但它没有找到与文件名匹配的文件Con.php

How can I fix this?

我怎样才能解决这个问题?

采纳答案by Ashutosh Jindal

Pathname too long problem

路径名过长问题

Given that the OP has already tried removing and re-adding the file without success, it is possible that the crux of the problem is that the complete path of Con.phphas hit the 260character limit in windows. However, when I tried to reproduce this problem, the error message clearly said fatal: unable to stat 'very/long/path/name': Filename too longso am not certain that that is the problem.

鉴于 OP 已经尝试删除和重新添加文件但没有成功,问题的症结可能在于Windows中的完整路径Con.php已达到260 个字符的限制。但是,当我尝试重现此问题时,错误消息明确指出,fatal: unable to stat 'very/long/path/name': Filename too long因此不确定这就是问题所在。

To check if this is the problem, switch to the directory containing Con.phpand execute the following:

要检查这是否是问题,请切换到包含Con.php并执行以下命令的目录

pwd -W | wc -c

That should give you a count of the number of characters in the windows path of the current directory. If the count is not>=260 then this is not the problem and I would recommend moving on to Debugging other issuessection below. However, if the count is >= 260, then proceed as follows:

这应该会为您计算当前目录的 Windows 路径中的字符数。如果计数>=260,那么这不是问题,我建议继续阅读Debugging other issues下面的部分。但是,如果计数 >= 260,则执行以下操作:

git config --system core.longpaths true

and then try adding the file again.

然后再次尝试添加文件。



See this in action below, which creates a path with >260 characters and the sets the above option to prove that it fixes the problem.

请参阅下面的操作,它创建了一个 > 260 个字符的路径,并设置了上述选项以证明它解决了问题。

$ git add 12345
fatal: unable to stat 'This/Is/A/Very/long/pathname/with/more/than/two/hundred/and/sixty/characters/This/Is/A/Very/long/pathname/with/more/than/two/hundred/and/sixty/characters/This/Is/A/Very/long/pathname/with/more/than/two/hundred/more/dir/12345': Filename too long

$ git config --system core.longpaths true
$ git add 12345  #Notice that there is no error now
$ git status     #Shows that the file was successfully staged

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   12345

Why is this configuration not the default?
If you were wondering about that, read this:

为什么这个配置不是默认的?
如果您对此感到疑惑,请阅读以下内容

Windows does not properly support files and directories longer than 260 characters. This applies to Windows Explorer, cmd.exe and many other applications (including many IDEs as well as bash, perl and tcl that come with Git for Windows).

Windows 无法正确支持长度超过 260 个字符的文件和目录。这适用于 Windows 资源管理器、cmd.exe 和许多其他应用程序(包括许多 IDE 以及 Windows 版 Git 附带的 bash、perl 和 tcl)。

and note this caveat:

并注意这个警告:

Scripted git commands may still fail with this option, so use at your own risk

使用此选项,脚本化的 git 命令可能仍会失败,因此使用风险自负



Debugging other issues

调试其他问题

If too-long-a-pathname is not the problem, then try executing the following command (of course changing git add 12345to git add app/CodeBehind/Con.php) and look at the ouptput for clues (and add this to your question to assist other readers in finding out the issue) :

如果太长的路径名不是问题,那么尝试执行以下命令(当然更改git add 12345git add app/CodeBehind/Con.php)并查看输出以寻找线索(并将其添加到您的问题中以帮助其他读者找出问题):

$ set -x; GIT_TRACE=2 GIT_CURL_VERBOSE=2 GIT_TRACE_PERFORMANCE=2 GIT_TRACE_PACK_ACCESS=2 GIT_TRACE_PACKET=2 GIT_TRACE_PACKFILE=2 GIT_TRACE_SETUP=2 GIT_TRACE_SHALLOW=2 git add 12345; set +x;

+ GIT_TRACE=2
+ GIT_CURL_VERBOSE=2
+ GIT_TRACE_PERFORMANCE=2
+ GIT_TRACE_PACK_ACCESS=2
+ GIT_TRACE_PACKET=2
+ GIT_TRACE_PACKFILE=2
+ GIT_TRACE_SETUP=2
+ GIT_TRACE_SHALLOW=2
+ git add 12345
16:06:04.269773 trace.c:333             setup: git_dir: .git
16:06:04.269773 trace.c:334             setup: git_common_dir: .git
16:06:04.269773 trace.c:335             setup: worktree: C:/Users/az/test-long-path-problems
16:06:04.269773 trace.c:336             setup: cwd: C:/Users/az/test-long-path-problems
16:06:04.269773 trace.c:337             setup: prefix: This/Is/A/Very/long/pathname/with/more/than/two/hundred/and/sixty/characters/This/Is/A/Very/long/pathname/with/more/than/two/hundred/and/sixty/characters/This/Is/A/Very/long/pathname/with/more/than/two/hundred/more/dir/
16:06:04.269773 git.c:350               trace: built-in: git 'add' '12345'
16:06:04.269773 trace.c:435             performance: 0.006209300 s: git command: 'git.exe' 'add' '12345'
+ set +x

回答by EKanadily

OK that happened to me and it turns out that the new windows "controlled folder" protection feature is blocking git. adding git to the allowed apps list solved the problem.

好吧,这发生在我身上,结果是新的 Windows“受控文件夹”保护功能阻止了 git。将 git 添加到允许的应用程序列表解决了这个问题。