Git in Visual studio code says file is modified even when there is no change
Asked Answered
I

7

59

I am using the Visual Studio Code for my salesforce development. I have some issues with Git as a repository. When I right-click on the package.xml and say Retrieve Source From Org, it is getting all the files from Org.

The problem is git extension is showing file is modified on the server even though I have a sample file in my local. I don't want to push all the files again and again to my git repo even when there is no change.

enter image description here

Any idea why this is happening and how to avoid it?

Update: I did git diff and it provides correct output. It only shows file changes that are modified. In this case only five.

Impostor answered 4/7, 2020 at 2:38 Comment(7)
what does command line git status tell you?Michelmichelangelo
I got similar problem recently. It only happen when I delete the whole folder and replace with similar folder. Then, vscode git also show files with no changes. But when I stage all, files with no changes are no longer shown in staging area.Ensoul
Is there a specific reason that you use Visual Studio Code? If you use it just because of solving conflicts or seeing git status, I can recommend another program.Paddock
git status displays the same list of files and says it is modifiedImpostor
Maybe VS Code reformat your files, can you share output of git diffPaddock
git diff gives the correct output. It only displays files that are actually modified.Impostor
Does this answer your question? VS code source control populates with all the files (code and zip files) from my User folder on MacOptometrist
I
31

I was able to resolve it by executing the following command

git config --global core.autocrlf false

Documentation: https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#:~:text=with%20these%20issues.-,core.autocrlf,-If%20you%E2%80%99re%20programming

Impostor answered 9/7, 2020 at 17:57 Comment(2)
Funnily I had to set core.autocrlf to true to make my "modified" files disappear.. I'm using a git repo checked out on Windows that gets opened in a VS Code dev container running in Ubuntu.Colorcast
This made it worse and made a file that didn't appear before as changed appear as changed even though it isn't 🤦Scanner
F
52

If we change the permission using then I also encountered that behavior. In order to overcome from this issue you can use the below commands.

git config core.filemode false  

In case you want to apply for the global.

git config --global core.filemode false

See also: What are the consequences of git config core.filemode false?

Fukuoka answered 13/1, 2021 at 12:19 Comment(5)
That works for me, but what are the consequences? Does this mean my committed .sh files won't be pulled as executable by others?Optometrist
This happens amongst others if you switch between Linux filesystem and Windows filesystem, for instance, if you switch from using VScode in CMD to VScode using WSL.Beria
I had this while switching between WSL and Windows. Installing Remote WSL extension solved this issue.Gentlemanfarmer
Take a look at https://mcmap.net/q/22310/-what-are-the-consequences-of-git-config-core-filemode-false if you want to understand what this does. @DanielKaplan kindly asked his above question and got a thorough answer.Membranophone
Faced sutiation when changed permitions to a folder on macOS. Solved my issue.Parmesan
I
31

I was able to resolve it by executing the following command

git config --global core.autocrlf false

Documentation: https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#:~:text=with%20these%20issues.-,core.autocrlf,-If%20you%E2%80%99re%20programming

Impostor answered 9/7, 2020 at 17:57 Comment(2)
Funnily I had to set core.autocrlf to true to make my "modified" files disappear.. I'm using a git repo checked out on Windows that gets opened in a VS Code dev container running in Ubuntu.Colorcast
This made it worse and made a file that didn't appear before as changed appear as changed even though it isn't 🤦Scanner
C
6

If you run git diff and see an output such as:

diff --git a/folder/file.tex b/folder/file.tex
old mode 100725
new mode 100614

Run the following command to fix the issue.

git config --unset core.filemode

If this doesn't work after refreshing source control in VS Code, run the following command as well.

git config --global core.filemode false
Cuspidor answered 24/4, 2022 at 1:1 Comment(1)
git config --unset core.filemode worked like a charm and brought back the actual state of the repositorySinistrorse
L
4

I had to do both the filemode and the autocrlf options above, plus,

git config core.whitespace cr-at-eol

I'm on a windows system with a linux subsystem (wsl), looking at a primarily made-with-linux codebase.

I also had to do those options both for the git installed in the linux subsystem and then again for the windows version of git, since VSCode used one but my actual git commands I enter into linux bash.

Lozano answered 7/3, 2022 at 23:22 Comment(0)
T
2

For me, these e command have fixed the issue:

git config --global core.filemode false
git config --global core.autocrlf false
git config --global core.whitespace cr-at-eol

https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#:~:text=with%20these%20issues.-,core.autocrlf,-If%20you%E2%80%99re%20programming

https://git-scm.com/docs/git-config#Documentation/git-config.txt-corefileMode

Translation answered 15/5, 2023 at 8:38 Comment(0)
O
1

If you are using cygwin and experiencing this issue, I have learned of a solution that doesn't require changing your git settings. First, be aware that this is a known issue but it is not planned on being fixed. After a bunch of research, I found this gist: https://gist.github.com/nickbudi/4b489f50086db805ea0f3864aa93a9f8 It consists of two files.

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

I will now explain the steps to use these scripts:

  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.

After those steps, only the modified files showed as modified in VS Code.

Optometrist answered 27/9, 2021 at 1:6 Comment(4)
I tried these scripts, they worked for basic git status display, but trying to stage/unstage a file resulted in errors. I.e. these commands failed: git add -A -- C:\project\hxcalc\package.json and git reset -q HEAD -- C:\project\hxcalc\package.json both results in fatal: C:\project\hxcalc\package.json: 'C:\project\hxcalc\package.json' is outside repository at '/mnt/c/project/hxcalc'. I tried the same commands in Cygwin bash, and indeed, Cygwin git doesn't seem to be able to handle Windows paths.Rigveda
What worked for me is this: github.com/nukata/cyg-git It's a tiny C wrapper that you compile with gcc (for my use case, regular Cygwin gcc worked), and a Zsh wrapper script that works similar to cygpath-git-vscode.bat, but also takes care of converting paths passed to git. I had to make a small change to this script to make it handle rev-parse commands that produce multiple lines of output (made a PR to the original repo).Rigveda
@GenePavlovsky Interesting. I don't think I can help with that because I do not get this error. I stage and unstage files with this setup.Optometrist
Can you try an add or reset comand (with a Windows path) such as I listed before, in Cygwin bash? Does it work for you?Rigveda
D
1

If you are using cygwin bash you should know that there is issue related to this one, instead you can use git bash for windows and set it as default shell in vscode.

Deadwood answered 21/6, 2022 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.