3 ms·
A git commit is a change from one state of the repository to another. A change is a diff, because... that's what a diff -- difference -- is, by definition. So
by kelnos 3y ago
A git commit is a change from one state of the repository to another. A change is a diff, because... that's what a diff -- difference -- is, by definition. So a commit is a diff. (However, the reverse is not true: a diff is not a commit! A diff can be a commit, but a diff can also be a representation of a series of commits, or something else entirely.)
A git tree is a snapshot. A snapshot is, by definition, a view of the world at a point in time. A commit doesn't show you the world: it only shows you what's changed between the previous world and the next world. The state of the tree -- a checkout that comprises all the files in a project, at a particular time, is a snapshot. I guess I can see how a commit might be confused for this: you can check out a tree at a particular commit, and that would be a snapshot. But the commit is not the tree! The commit is not the snapshot! You could also look at it this way: you are not checking out a commit; you are checking out a tree that happens to be identified by the ref that points to a commit. (Technically that's not even the case, or rather it is, but only coincidentally: while we can check out trees by giving git a commit ref, in reality the tree itself has a different object name.)
To relate the two paragraphs above, a commit is the description of the change between two different trees (aka snapshots).
Note that I am deliberately ignoring how git actually implements these things, as I don't think git's implementation is remotely useful as a mental model. Consider: git implements commits as snapshots, but is your mental model of "making a commit" that you make a fresh, empty directory, and then retype all your code from scratch, but with the changes you want to make in the commit? No, of course not, that's nonsense. (Ok, maybe that's too extreme, but I think it's still nonsense to think about it as making a copy of your repository, making your changes to the copy, and then storing the entire new copy.)
A (single) commit is sort of a history, but not really in a useful sense. To me, history is a progression over time. Sure, a single commit gives you the "history" (meh) of a single change, over a short period of time. But when I think of my "git history" or my "commit history", I'm thinking about a series of changes (commits) over some longer period of time. Like "the list of commits between these two tags show the history of changes between these two releases", or, more generally, "all the commits on the main branch show the entire history of the project".
> is “how Git implements it” really the right way to explain it?
No, I don't think so. Implementation details (in the general sense, not specific to git) often don't represent the mental model that's most useful for understanding a system. Git is a little interesting in that it actually is a lot easier to understand how to use git and be productive using git if you understand a bit about its implementation. But that doesn't mean your mental model of a version control system should depend on implementation details.