Git diff fails on file with underscore in path

≡放荡痞女 提交于 2019-12-07 16:13:57

问题


I'm running Windows 7, and I have the latest version of git (2.7.2.windows.1). I previously used Windows PowerShell as my terminal for git operations, with no issues. Recently, I decided to switch to Git Bash, but I'm having one major issue.

My project has a directory (within which are many subdirectories) whose name is simply an underscore. Whenever I try to run git diff on a file within that directory, I get the following error:

$ git diff _/css/templates/jquery.tag-editor.css fatal: Invalid object name '_C'.

As far as I know, an underscore is a perfectly valid character in a file/directory name, and tab completion works fine within that directory, so I know the terminal can "see" inside it. Additionally, other contributors to the project, all of them running OSX, do not have this problem. And when I run a simple git diff, without specifying any single file, it works fine, and happily includes the diff for any changed files within the underscore directory. It also works if I cd into the underscore directory and run the git diff from there, so that the path I pass to it does not include the underscore.

What exactly is happening here to prevent me from running git diff on these files? And where does the "C" come from in the error message when I try to do so?

Update

When I run git checkout -- _/css/templates/jquery.tag-editor.css to discard the changes to that file, this is the error I see:

error: pathspec '_C:/Program Files/Git/css/templates/jquery.tag-editor.css' did not match any file(s) known to git.

C:\Program Files\Git is directory of my Git installation. So apparently part of the path is being interpreted as referring to the Git installation directory? Again, what is causing this to happen?


回答1:


The problems you are experiencing are based on the Posix Path conversions of MinGW/Msys2.

  • on git-bash try to double a slash: git diff _//css/templates/jquery.tag-editor.css or use backslashes (which need to be escaped in git-bash): git diff _\\css\\templates\\jquery.tag-editor.css or prepend MSYS_NO_PATHCONV=1: MSYS_NO_PATHCONV=1 git diff _/css/templates/jquery.tag-editor.css
  • on CMD you should use a backslash instead of a slash: git diff _\css\templates\jquery.tag-editor.css OR double a slash as for git-bash

in order to prevent the conversion.



来源:https://stackoverflow.com/questions/35660576/git-diff-fails-on-file-with-underscore-in-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!