Chapter 6. Commits
In Git, a commit is used to record changes to a repository.
At face value, a Git commit seems no different from a commit or check-in found in other VCS. Under the hood, however, a Git commit operates in a unique way.
When a commit occurs, Git records a snapshot of the index and places that snapshot in the object store. (Preparing the index for a commit is covered in Chapter 5.) This snapshot does not contain a copy of every file and directory in the index, because such a strategy would require enormous and prohibitive amounts of storage. Instead, Git compares the current state of the index to the previous snapshot and so derives a list of affected files and directories. Git creates new blobs for any file that has changed and new trees for any directory that has changed, and it reuses any blob or tree object that has not changed.
Commit snapshots are chained together, with each new snapshot pointing to its predecessor. Over time, a sequence of changes is represented as a series of commits.
It may seem expensive to compare the entire index to some prior state, yet the whole process is remarkably fast because every Git object has an SHA1 hash. If two objects, even two subtrees, have the same SHA1 hash, the objects are identical. Git can avoid swaths of recursive comparisons by pruning subtrees that have the same content.
There is a one-to-one correspondence between a set of changes in the repository and a commit: A commit is the only method of introducing changes to a ...
Get Version Control with Git, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.