On Tue, 25 Mar 2014 01:35:11 -0700 (PDT) Gabriele Salvati <[email protected]> wrote:
> I'd like to know how git restore files when checkout is executed. > For each commit git knows the diff (added and deleted lines) with the > parent commit. If git use only diff it must start from the init and > go forward until the current commit. This solution could be very > slowly. I suppose git use different approach for calculating the > content of a file in a given commit. > > Can someone explain how checkout works? Git does not store diffs. At least not conceptually. Instead, conseptually each commit references a full snapshot of the entire project. Hence given a commit, Git works like this: 1. Get the SHA-1 name of the tree object representing the root directory of the project out of that commit object. 2. Locate, read and parse that tree object to obtain SHA-1 names of blobs representing snapshots of data of all the top-level files and SHA-1 names of trees representing the nested directories. 3. The step (2) is then recursively repeated for each nested tree object. As you can see, since in a typical project only a fraction of data is changed in each commit, this storage format is rather efficient: most trees and blobs are not changed from commit to commit and so different commits reference the same data. But Git goes further than that and when it detects the number of such "loose" objects in the repository is beyond a certain (configurable) threshold it packs (using xdelta compression [1]) most of these objects into the so-called "pack files" which are indexed archives. These are not really "diffs" in the `git diff` sense, of course, but are rather binary deltas as you'd find in a typical compressor/archival program. To get better grasp on these concepts I'd really recommend to start with explanatory guides: [2], [3], [4]. And if you find yourself lost in those you can even start with [5]. 1. http://en.wikipedia.org/wiki/Delta_encoding#Git 2. http://newartisans.com/2008/04/git-from-the-bottom-up/ 3. http://eagain.net/articles/git-for-computer-scientists/ 4. http://gitolite.com/gcs.html 5. http://tom.preston-werner.com/2009/05/19/the-git-parable.html -- You received this message because you are subscribed to the Google Groups "Git for human beings" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
