Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does git detect that a file has been modified?

Tags:

git

How does git detect a file modification so fast?

Does it hash every file in the repo and compare SHA1s? This would take a lot of time, wouldn't it?

Or does it compare atime, ctime or mtime?

like image 486
hdorio Avatar asked Nov 22 '09 14:11

hdorio


People also ask

How does git determine differences?

Automatic identification of the differences between two versions of a file is a common and basic task in several applications of mining code repositories. Git, a version control system, has a diff utility and users can select algorithms of diff from the default algorithm Myers to the advanced Histogram algorithm.

How does git maintain integrity of files?

There is no central repository. Git has integrity, meaning each file is checked (summed) to be sure there was no bit loss during any file manipulation by git. Each snapshot (also called commit) has a unique identifier.

How do you check the changes in git before commit?

If you just want to see the diff without committing, use git diff to see unstaged changes, git diff --cached to see changes staged for commit, or git diff HEAD to see both staged and unstaged changes in your working tree.


1 Answers

Git tries hard to get convinced from the lstat() value alone that the worktree matches the index, because falling back on file contents is very expensive.

Documentation/technical/racy-git.txt describes what stat fields are used, and how some race conditions due to low mtime granularity are avoided. This article has some more detail.

stat values aren't tamper-proof, see futimens(3). Git may be fooled into missing a change to a file; that does not compromise the integrity of content-hashing.

like image 115
Tobu Avatar answered Sep 18 '22 08:09

Tobu