How does git track file changes internally?

前端 未结 5 1986
孤街浪徒
孤街浪徒 2021-02-12 16:10

Could somebody explain how git knows internally that files X, Y and Z have changed? What is the process behind the scenes that recognizes when a file has not yet been added or h

5条回答
  •  一个人的身影
    2021-02-12 16:39

    If the answer in the possible duplicate doesn't suffice you might want to take a look at this http://www.geekgumbo.com/2011/07/19/git-basics-how-git-saves-your-work/

    To make a long story short, Git uses the SHA-1 of the file contents to keep track of changes. Git keeps track of four objects: a blob, a tree, a commit, and a tag.

    To answer your question on how it keeps track of changes here's a quote from that link:

    The tree object is how Git keeps track of file names and directories. There is a tree object for each directory. The tree object points to the SHA-1 blobs, the files, in that directory, and other trees, sub-directories at the time of the commit. Each tree object is encrypted into, you guessed it, a SHA-1 hash of its contents, and stored in .git/objects. The name of the trees, since they are SHA-1 hashes, allow Git to quickly see if there's been any changes to any files or directories by comparing the name to the previous name. Pretty slick.

提交回复
热议问题