Friday, June 19, 2020

"git status" shows modifications, but "git diff" shows nothing

I encountered a strange problem about a git repository today, that is, when I do a git status, git reports there are modifications. However, when I do a git diff, git reports no difference, like the following,

$ git status .
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   .gitignore

$ git diff
$

After a little bit digging, apparently a few others encountered the same problem as a Stack Overflow discussion indicates.

The cause of my case are two,
  1. Some files have different line endings from the version in the repository as a result that I was working on a Windows Subsystem for Linux and had the git's configuration property core.autocrlf set as input. I revealed the Windows line ending using vim, as in the following,
    
    $ vim -b .gitignore
    # Compiled class file^M
    *.class^M
    ^M
    # Log file^M
    *.log^M
    ^M
    # BlueJ files^M
    *.ctxt^M
    

    where the ^M characters are the Windows line endings, i.e., CR LF. I got rid of the line endings using sed, as in,
    
     sed -i -e s/^M// .gitignore
    

    Note that we enter the line ending by pressing CRTL-V CRTRL-M
  2. The second issue is that some files have different permissions from those in the repository. Since I was using a Windows Subsystem for Linux on a Windows host, it is difficult for me to set the permissions identical to those in the repository. However, we can let git to ignore permission difference, i.e.,
    
    git config --local core.filemode false
    

    where I only did this for the repository. If we wish to do this for globally for all repositories, just issue the command without the --local option.


1 comment: