Davy Davy - 2 months ago 7
Git Question

Git: no difference when file timestamp and size are same

[Operation]



1. Install git on windows 7
2. Init a respo
3. Add a new text file name "a.txt" and commit
4. Copy a.txt to another folder:
4.1 Change one word in txt file, keep the same file size
4.2 Change file time stamp to original
5. Copy updated a.txt back to the git respo just created
6. Check use command "git status" or "git diff"


RESULT: No difference will be found by git

My expectation is git could find the file's difference.
I want to know why and how?

PS:
1.If step 4.1, 4.2 operate in folder of respo, git could find the file changes.
2.If time stamp set the future's date, git also could run well

[Sample Command]



Set PATH=%PATH%;D:\Git\bin

git init Test
cd Test

echo 123456 > a.txt
D:\XXX\FILETIME.exe a.txt 09/29/2016 12:12:12

git add .
git commit -m "init a.txt"

echo 654321 > a.txt
D:\XXX\FILETIME.exe a.txt 09/29/2016 12:12:12

git status
git diff


[LOG]



C:\>git init Test
Initialized empty Git repository in C:/Test/.git/

C:\>cd Test
C:\Test>echo 123456 > a.txt

C:\Test>D:\xxx\FILETIME.exe a.txt 09/29/2016 12:12:12
a.txt
Creation time ... 09/29/2016 12:12:12
Last access time ... 09/29/2016 12:12:12
Last write time ... 09/29/2016 12:12:12

C:\Test>git add .

C:\Test>git commit -m "init a.txt"
[master (root-commit) 95f91e5] init a.txt
1 file changed, 1 insertion(+)
create mode 100644 a.txt

C:\Test>echo 654321 > a.txt

C:\Test>D:\xxx\FILETIME.exe a.txt 09/29/2016 12:12:12
a.txt
Creation time ... 09/29/2016 12:12:12
Last access time ... 09/29/2016 12:12:12
Last write time ... 09/29/2016 12:12:12

C:\Test>git status
On branch master
nothing to commit, working tree clean

C:\Test>git diff

Answer

Git tries to stat() the files instead of accessing to the whole content of every file everytime you run a command using the worktree (git status or git diff for instance).

Doing otherwise would be extremly costly, and won't solve any case that would happen in any normal case.