1

for example, by git diff file, we can know that we deleted 3 lines, inserted 1 line and modified 3 lines. but how can git achieve it ? Does it track the process of my editting? Does vim leave some special files for git to achiebe it?

I mean, if I modified one line, I can also say that I delete one line and insert one line. How does git know that I modified this line rather than doing deletion and insertion?

youkaichao
  • 1,938
  • 1
  • 14
  • 26
  • You should review a good Git tutorial to get a thorough answer to your questions. For your second question, the answer is that sometimes Git sees it as a deletion followed by insertion, and other times it sees it as a modification. – Tim Biegeleisen Jun 25 '18 at 15:45
  • https://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide – dimwittedanimal Jun 25 '18 at 15:53

1 Answers1

5

Does it track the process of my editing?

No.

It checks for the modifications by comparing the cached version (stored in the .git folder) and the current version.

Does vim leave some special files for git to achieve it?

No

... How does git know that I modified this line rather than doing deletion and insertion?

In short, it checks for the similarities as well as the differences in two files by using an algorithm that answers the longest common subsequence problem. It's important to know that diff is not a command only present in git - it is also a Linux/Unix command.

There are many algorithms that can be used to find the difference between two files, such as the Hunt–McIlroy algorithm and Myer's diff algorithm.

Rather than looking for a specific answer for git diff, I suggest you look into the two above algorithms instead.

See this question for more details: Myers diff algorithm vs Hunt–McIlroy algorithm

TwiN
  • 3,554
  • 1
  • 20
  • 31
  • yeah, I know about the longest common subsequence problem, but it is about the character level. I mean, you can have a word **aaabbbccc** and modify it to be **xyzabcdef** , the LCS is **abc** but LCS doesn't mean anything. – youkaichao Jun 25 '18 at 15:55
  • Astute observation -- there is more than just an algorithm being used. For instance, the line number is also being taken in consideration. So even if `abcdef` becomes `ghijkl`, because the line number matches, it will be considered as a modification rather than an insertion/deletion. – TwiN Jun 25 '18 at 15:59
  • 1
    If you want, you can check git's diff source code here: https://github.com/git/git/blob/master/diff.c – TwiN Jun 25 '18 at 16:04