How does git store files?

前端 未结 2 1909
一个人的身影
一个人的身影 2020-11-22 06:36

I just started learning git and to do so I started reading the Git Community Book, and in this book they say that SVN and CVS store the difference between files and that git

相关标签:
2条回答
  • 2020-11-22 06:54

    Git does include for each commit a full copy of all the files, except that, for the content already present in the Git repo, the snapshot will simply point to said content rather than duplicate it.
    That also means that several files with the same content are stored only once.

    So a snapshot is basically a commit, referring to the content of a directory structure.

    Some good references are:

    • git.github.io/git-reference

    You tell Git you want to save a snapshot of your project with the git commit command and it basically records a manifest of what all of the files in your project look like at that point

    • git immersion

    Lab 12 illustrates how to get previous snapshots

    • "You could have invented git (and maybe you already have!)"

    • What is a git “Snapshot”?

    • Learn GitHub


    The progit book has the more comprehensive description of a snapshot:

    The major difference between Git and any other VCS (Subversion and friends included) is the way Git thinks about its data.
    Conceptually, most other systems store information as a list of file-based changes. These systems (CVS, Subversion, Perforce, Bazaar, and so on) think of the information they keep as a set of files and the changes made to each file over time

    delta-based VCS

    Git doesn’t think of or store its data this way. Instead, Git thinks of its data more like a set of snapshots of a mini filesystem.
    Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot.
    To be efficient, if files have not changed, Git doesn’t store the file again—just a link to the previous identical file it has already stored.
    Git thinks about its data more like as below:

    snapshot-based VCS

    This is an important distinction between Git and nearly all other VCSs. It makes Git reconsider almost every aspect of version control that most other systems copied from the previous generation. This makes Git more like a mini filesystem with some incredibly powerful tools built on top of it, rather than simply a VCS.


    Jan Hudec adds this important comment:

    While that's true and important on the conceptual level, it is NOT true at the storage level.
    Git does use deltas for storage.
    Not only that, but it's more efficient in it than any other system. Because it does not keep per-file history, when it wants to do delta compression, it takes each blob, selects some blobs that are likely to be similar (using heuristics that includes the closest approximation of previous version and some others), tries to generate the deltas and picks the smallest one. This way it can (often, depends on the heuristics) take advantage of other similar files or older versions that are more similar than the previous. The "pack window" parameter allows trading performance for delta compression quality. The default (10) generally gives decent results, but when space is limited or to speed up network transfers, git gc --aggressive uses value 250, which makes it run very slow, but provide extra compression for history data.

    0 讨论(0)
  • 2020-11-22 07:11

    Git logically stores each file under its SHA1. What this means is if you have two files with exactly the same content in a repository (or if you rename a file), only one copy is stored.

    But this also means that when you modify a small part of a file and commit, another copy of the file is stored. The way git solves this is using pack files. Once in a while, all the “loose” files (actually, not just files, but objects containing commit and directory information too) from a repo are gathered and compressed into a pack file. The pack file is compressed using zlib. And similar files are also delta-compressed.

    The same format is also used when pulling or pushing (at least with some protocols), so those files don't have to be recompressed again.

    The result of this is that a git repository, containing the whole uncompressed working copy, uncompressed recent files and compressed older files is usually relatively small, two times smaller than the size of the working copy. And this means it's smaller than SVN repo with the same files, even though SVN doesn't store the history locally.

    0 讨论(0)
提交回复
热议问题