git没有这样的文件或目录

git没有这样的文件或目录

问题描述:

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

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

下面是输入和输出,希望您能为我提供帮助:

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

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

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.

我该如何解决?

路径名过长的问题

鉴于OP已经尝试删除并重新添加文件而没有成功,问题的症结可能是 Con.php 的完整路径已经击中 260 字符数限制在Windows中.但是,当我尝试重现此问题时,错误消息明确指出 fatal:无法统计'very/long/path/name':文件名太长,因此不确定这是问题所在.

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.php has hit the 260 character 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 long so am not certain that that is the problem.

要检查是否存在此问题,请切换到包含 Con.php 的目录并执行以下操作:

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

pwd -W | wc -c

这应该可以为您计算当前目录的Windows路径中的字符数.如果计数为 not > = 260,那么这不是问题,我建议继续进行下面的 Debugging other issue 部分.但是,如果计数> = 260,则请按照下列步骤操作:

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 issues section below. However, if the count is >= 260, then proceed as follows:

git config --system core.longpaths true

,然后尝试再次添加文件.

and then try adding the file again.

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

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.

$ 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不正确支持文件和目录的时间超过260个字符.这适用于Windows资源管理器,cmd.exe和许多其他应用程序(包括许多IDE以及bash,perl和tcl适用于Windows的Git).

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).

并注意以下警告:

使用此选项的脚本git命令可能仍会失败,因此请在您的计算机上使用自负

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


调试其他问题

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


Debugging other issues

If too-long-a-pathname is not the problem, then try executing the following command (of course changing git add 12345 to 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) :

$ 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