git status shows changed files but git diff doesn't

走远了吗. 提交于 2019-11-27 19:46:36

问题


I've had a look at all similar questions however I've double checked and something strange is definitely happening.

On one server (Solaris with git 1.8.1) I cloned the git repository then copied the .git folder into my existing live files. This worked perfectly, I could run

git status

then

git diff [filename]

to check any files that were different.

On another server (Solaris with git 1.7.6) I'm doing exactly the same however

git diff [filename] 

shows nothing, even if the contents of the file is definitely different. I have also tested adding a new file, committing it then editing. Same issue, git status shows the file as changed but git diff shows nothing. If I download the changed file and run a diff locally then I get diff output.


回答1:


I added the file to the index:

git add file_name

and then ran:

git diff --cached file_name

You can see the description of git diff here.

If you need to undo your git add, then please see here: How to undo 'git add' before commit?




回答2:


There are a few reasons why git status might show a difference but git diff might not.

  • The mode (permission bits) of the file changed-- for example, from 777 to 700.

  • The line feed style changed from CRLF (DOS) to LF (UNIX)

The easiest way to find out what happened is to run git format-patch HEAD^ and see what the generated patch says.




回答3:


For me, it had something to do with file permissions. Someone with Mac/Linux on my project seems to commit some files with non-default permissions which my Windows git client failed to reproduce. Solution for me was to tell git to ignore file permissions:

git config core.fileMode false

Other insight: How do I make Git ignore file mode (chmod) changes?




回答4:


I had an issue where hundreds of line endings were modified by some program and git diff listed all source files as changed. After fixing the line endings, git status still listed the files as modified.

I was able to fix this problem by adding all files to index and then resetting the index.

git add -A
git reset

core.filemode was set to false.




回答5:


I suspect there is something wrong either with your git installation or your repository.

Try running:

GIT_TRACE=2 git <command>

See if you get anything useful. If that doesn't help, just strace and see what's going wrong:

strace git <command>



回答6:


I had a similar problem: git diff would show differences, but git diff <filename> would not. It turned out that I set LESS to a string including -F (--quit-if-one-screen). Removing that flag solved the problem.




回答7:


Ran into this problem. My case was similar to LESS issue posted by @rcwxok.

In my case, I set the PAGER environment var to PAGER='less -RSF'.

However, unlike the previous answers, I didn't want to remove the -F option because I explicitly put it there hoping to prevent showing the diff in less if it's shorter than a screenful.

To get the desired result, instead of removing -F, I added -X: PAGER='less -RSFX'. This both solved the git diff issue and in addition it prevents showing short diffs with less.

Hope this helps someone.




回答8:


Short Answer

Running git add sometimes helps.

Example

Git status is showing changed files and git diff is showing nothing...

> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   package.json

no changes added to commit (use "git add" and/or "git commit -a")
> git diff
> 

...running git add resolves the inconsistency.

> git add
> git status
On branch master
nothing to commit, working directory clean
> 



回答9:


As already noted above, this situation may arise due to line-ending problems (CRLF vs LF). I solved this problem (under git version 2.22.0) with this command:

git add --renormalize .

According to the manual:

       --renormalize
           Apply the "clean" process freshly to all tracked files to
           forcibly add them again to the index. This is useful after
           changing core.autocrlf configuration or the text attribute in
           order to correct files added with wrong CRLF/LF line endings.
           This option implies -u.



回答10:


I've just run in a similar issue. git diff file showed nothing because I added file to the git index with some part of it's name in uppercase: GeoJSONContainer.js. Afterwards, I've renamed it to GeoJsonContainer.js and changes stopped being tracked. git diff GeoJsonContainer.js was showing nothing. Had to remove file from index with a force flag, and add the file again:

git rm -f GeoJSONContainer.js 
git add GeoJSONContainer.js



回答11:


You did not really ask an actual question, but since this is a general use case I'm using quite often here's what I do. You may try this yourself and see if the error persists.

My assumption on your use case: You have an existing directory containing files and directories and now want to convert it into a git repository that is cloned from some place else without changing any data in your current dir.

There are really two ways.

Clone repo - mv .git - git reset --hard

This method is what you did - to clone the existing repo into an empty dir, then move the .git dir into the destination directory. To work without problems, this generally requires you then to run

git reset --hard

However, that would change the state of files in your current directory. You can try this on a full copy/rsync of your directory and study what changes. At least afterwards you should no longer see discrepancies between git log and status.

Init new repo - point to origin

The second is less disturbing: cd into your destination, and start a new repo with

git init

Then you tell that new repo, that it has an ancestor some place else:

git remote add origin original_git_repo_path

Then safely

git fetch origin master

to copy over the data without changing your local files. Everything should be fine now.

I always recommend the second way for being less error prone.




回答12:


I had this same problem described in the following way: If I typed

$ git diff 

git simply returned to the prompt with no error.

If I typed

$ git diff <filename>

git simply returned to the prompt with no error.

Finally, by reading around I noticed that git diff actually calls the mingw64\bin\diff.exe to do the work.

Here's the deal. I'm running windows and had installed another bash utility and it changed my path so it no longer pointed to my mingw64\bin directory.

So if you type: git diff and it just returns to the prompt you may have this problem.

The actual diff.exe which is run by git is located in your mingw64\bin directory

Finally, to fix this I actually copied my mingw64\bin directory to the location git was looking for it in. I tried it and it still didn't work.

Then, I closed my git bash window and opened it again went to my same repo that was failing and now it works.

Hope this helps you too.




回答13:


I stumbled upon this problem again. But this time it occurred for a different reason. I had copied files into the repo to overwrite the previous versions. Now I can see the files are modified but diff doesn't return the diffs.

For example, I have a mainpage.xaml file. In File Explorer I pasted a new mainpage.xaml file over the one in my current repo. I did the work on another machine and just pasted the file here.

The file shows modified, but when I run git diff, it will not show the changes. It's probably because the fileinfo on the file has changed and git knows that it isn't really the same file. Interesting.

You can see that when I run diff on the file it shows nothing, just returns the prompt.



来源:https://stackoverflow.com/questions/14564946/git-status-shows-changed-files-but-git-diff-doesnt

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