Visual Studio Code中的Git说即使没有更改文件也已修改文件。

57

我正在使用Visual Studio Code进行Salesforce开发,但在使用Git作为版本库时遇到了一些问题。当我右键单击package.xml并选择从组织中检索源代码时,它会从组织中获取所有文件。

问题是Git扩展程序显示文件已在服务器上修改,即使我在本地有一个样本文件。即使没有更改,我也不想一遍又一遍地将所有文件推送到我的Git仓库中。

enter image description here

有什么想法可以解决这个问题吗?

更新: 我执行了git diff并提供了正确的输出。它只显示已修改的文件更改。在这种情况下,只有五个文件被修改。


1
命令行 git status 告诉你什么? - rioV8
1
git status 显示相同的文件列表并表示已修改。 - Shaggy
也许VS Code重新格式化了您的文件,您能分享一下git diff的输出吗? - Prihex
git diff 给出了正确的输出。它只显示实际修改过的文件。 - Shaggy
显示剩余2条评论
7个回答

51
如果我们使用更改权限,那么我也遇到了这种行为。为了解决这个问题,您可以使用以下命令。
git config core.filemode false  

如果你想申请全球,请注意以下事项。
git config --global core.filemode false

另请参阅:git config core.filemode false的后果是什么?


5
可以,但后果是什么?这是否意味着其他人无法将我提交的.sh文件作为可执行文件拉取? - Daniel Kaplan
7
如果你在Linux文件系统和Windows文件系统之间切换,例如从CMD中使用VScode切换到使用WSL中的VScode,那么就会出现这种情况。请注意,这只是其中一种可能性。 - anneb
5
在切换WSL和Windows时,我遇到了这个问题。安装Remote WSL扩展解决了这个问题。 - dinesh ygv
3
如果您想了解这段代码的作用,请查看 https://dev59.com/SVEG5IYBdhLWcg3wQYM6#69341082。@DanielKaplan提出了他上面的问题,并得到了详细的答复。 - pwb2103
遇到了在 macOS 上更改文件夹权限的情况。解决了我的问题。 - MadaShindeInai

27

8
有趣的是,我不得不将core.autocrlf设置为true,才能使我的“修改”文件消失。我正在使用在Windows上检出并在运行Ubuntu的VS Code开发容器中打开的git仓库。 - stmax

4
如果你运行 git diff 命令,会看到以下输出内容:
diff --git a/folder/file.tex b/folder/file.tex
old mode 100725
new mode 100614

运行以下命令以解决此问题。
git config --unset core.filemode

如果在VS Code中刷新源代码后仍然无法工作,请运行以下命令。

git config --global core.filemode false

1
git config --unset core.filemode 像魔法一样奏效,恢复了仓库的实际状态。 - LukeSavefrogs

4

我必须执行上述filemodeautocrlf选项,以及

git config core.whitespace cr-at-eol

我使用的是安装了Linux子系统(WSL)的Windows系统,查看主要使用Linux构建的代码库。

我还必须为安装在Linux子系统中的Git和Windows版本的Git执行这些选项,因为VSCode使用其中一个,但我实际输入到Linux Bash中的Git命令使用另一个。


1

如果您使用的是cygwin并且遇到了此问题,我已经学到了一个解决方案,它不需要更改您的git设置。首先,请注意这是一个已知问题,但不打算修复。 经过大量的研究,我找到了这个gist:https://gist.github.com/nickbudi/4b489f50086db805ea0f3864aa93a9f8 它由两个文件组成。

cygpath-git-editor.sh:

#!/bin/bash

# wrapper to convert linux paths to windows
# so vscode will work as a git editor with cygwin
# editor="/home/this/file.sh" in .gitconfig

# extract last argument (the file path)
for last; do true; done

# get all the initial command arguments
all="${@:1:$(($#-1))}"

# launch editor with windows path
code $all $(cygpath -w $last) 

cygpath-git-vscode.bat:

@echo off

REM wrapper to convert linux paths to windows
REM so vscode git integration will work with cygwin
REM "git.path"="C:\\this\\file.bat" in settings.json

setlocal
set PATH=C:\cygwin\bin;%PATH%

if "%1" equ "rev-parse" goto rev_parse
git %*
goto :eof
:rev_parse
for /f %%1 in ('git %*') do cygpath -w %%1

我现在将解释如何使用这些脚本的步骤:

  1. Copy and paste the scripts to your local file system.

  2. In cygpath-git-vscode.bat, change set PATH=C:\cygwin\bin;%PATH% to the correct path on your system. For me, that meant setting it to set PATH=C:\cygwin64\bin;%PATH%

  3. Run chmod +x <filename> on each file. If you forget this step, you'll get a vague error about the command not being found.

  4. Edit your visual studio code settings.json and set git.path to the windows path to cygpath-git-vscode.bat. e.g.,

    "git.path": "C:\\cygwin64\\home\\user\\cygpath-git\\cygpath-git-vscode.bat"
    
  5. Restart visual studio code.

在执行了这些步骤后,只有修改过的文件在VS Code中显示为已修改。


我尝试了这些脚本,它们可以用于基本的git状态显示,但尝试对文件进行暂存/取消暂存操作时出现了错误。也就是说,这些命令失败了:git add -A -- C:\project\hxcalc\package.jsongit reset -q HEAD -- C:\project\hxcalc\package.json 都导致了 fatal: C:\project\hxcalc\package.json: 'C:\project\hxcalc\package.json' is outside repository at '/mnt/c/project/hxcalc'。我在Cygwin bash中尝试了相同的命令,确实,Cygwin git似乎无法处理Windows路径。 - undefined
对我来说有效的方法是使用这个:https://github.com/nukata/cyg-git 这是一个小型的C包装器,你可以使用gcc编译(对于我的用例,常规的Cygwin gcc可以工作),还有一个类似于cygpath-git-vscode.bat的Zsh包装器脚本,但它还负责转换传递给git的路径。我不得不对这个脚本进行了一点修改,以使其处理产生多行输出的rev-parse命令(我向原始仓库提交了一个PR)。 - undefined
@GenePavlovsky 有趣。我不认为我能帮上忙,因为我没有遇到这个错误。我使用这个设置来进行文件的暂存和取消暂存。 - undefined
你能在Cygwin的bash中尝试一下我之前列出的添加或重置命令(带有Windows路径)吗?对你有效吗? - undefined

1

如果您正在使用cygwin bash,您应该知道与此相关的问题,相反,您可以使用Windows的git bash,并将其设置为vscode的默认shell。


1

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