6 ms·
Commits are shapshots, not diffs
- stolee 6y agoWe fixed the typo in the blog title (shapshots). Sorry!
- recursive 6y agoI have never understood cherry-pick for this very reason. This helps, but I'm still confused. > The git cherry-pick <oid> command creates a new commit with an identical diff to <oid> whose parent is the current commit. How can two diffs ever be considered equivalent when they include a changed file that had different starting contents? Can they?
- stolee 6y agoIf I have a 100-line file and on 'main' it changes near the top, but in my 'topic' branch it changes near the bottom, then I can cherry-pick 'topic' onto 'main' and Git will resolve the diff correctly. The resulting diff or patch would only change in the line numbers for the context of the diff. This is of course a very simple example. You might hit a conflict in your "git cherry-pick" command which gives you an opportunity to resolve the unexpected diff issue in an appropriate way, which ends up with a different diff than before.
- recursive 6y agoI kind of intuitively get it, but that doesn't really seem well defined. I'm always a little bit spooked that `cherry-pick` will cleanly apply when it really shouldn't have. It's not clear to me under which circumstances it automatically resolves.
- tsimionescu 6y agoYou're right to be spooked about that, but you're wrong if you think only cherry-pick has this problem. In fact, all git commands can and sometimes will cleanly apply and subtly mess your files (git merge, git pull, git rebase, git apply, git stash apply etc). The definition of how changes are applied actually has nothing to do with git itself, and everything to do with the diff algorithm you choose (of course, you normally use a built-in one, but I believe you can customize it if you really want). In general, the default Git diff algorithm, like all text-based diff algorithms, can have problems with structured data, such as removing closed parens or significant white-space. Naturally, it can also be problematic if you have declarations that must be a unique in a file, but that can occur in different places. The Java or Go `package` statements are safe, since they must occur at the beginning of a file, so if they are different between the 2 files they are likely to be caught. But if two people have added a top-level function called `foo` , but they added it in different places in the file with different params, it's pretty likely that the diff algorithm will not see any conflict and will duplicate both lines. Cherry pick is in fact one of the places I would normally worry least about this, since it is usually done for limited sized commits. However, when merging a feature branch into master, the potential for errors goes up, and so does the work required to catch such errors during the review.
- recursive 6y agoThank you. In all my (extensive) commentary in this topic, I feel this is the first response that addresses the root of my confusion in a way I can understand. Sincerely, this is helpful.
- tsimionescu 6y agoGlad to hear that! Rarely have I actually felt that a comment I wrote actually made a difference.
- ninkendo 6y agoYou don’t even need to think about diff algorithms to see why a cherry-pick, merge, etc may not do what you want. If in my branch, I rename oldFunc to newFunc in file A, and change file B to replace the call to oldFunc with newFunc; and in your branch, you add a new call to oldFunc in file C... the code will break when we merge our branches. Our changes would both pass tests independently, but would break when we merge them. No file-level diff algorithm would detect a “conflict” here. Diff algorithms only help with saying “are two branches trying to edit the same lines of code”, but the answer to that question is never enough to tell you whether two changes logically will apply cleanly to one another.
- pmeunier 6y ago> If I have a 100-line file and on 'main' it changes near the top, but in my 'topic' branch it changes near the bottom, then I can cherry-pick 'topic' onto 'main' and Git will resolve the diff correctly. That is not true: sometimes Git will take the new lines from "topic" and merge them into the new lines from "main", see https://pijul.org/manual/why_pijul.html https://pijul.org/manual/why_pijul.html > You might hit a conflict in your "git cherry-pick" command which gives you an opportunity to resolve the unexpected diff issue in an appropriate way, which ends up with a different diff than before. Sometimes when you cherry-pick, you might not even hit a "true" conflict, but if you forgot to run "rerere", you might simply hit a previously solved conflict again.
- MaxBarraclough 6y agoYou're right that the commit produced by the cherry-pick operation won't be identical to the commit being cherry-picked. It's the diffs that are identical, not the final result. It's analogous to how the difference between 5 and 15 is equal to the difference between 105 and 115.
- recursive 6y agoThat doesn't help. The result of subtraction is defined to be a number. The result of a diff is... a nebulously defined concept that somehow describes the changes. It's not clearly (to me) defined which parts of the files are considered to part of the diff and which parts are to be excluded.
- MaxBarraclough 6y ago> The result of subtraction is defined to be a number. A diff shows the difference between two commits (i.e. snapshots). Perhaps a better math analogy: one point can be subtracted from another to give a vector. > It's not clearly (to me) defined which parts of the files are considered to part of the diff and which parts are to be excluded. You're right that it's not precisely defined in that way, but there's a good reason for this. If you move the top line of a file to the bottom, should the diff tool show you that one line was deleted and one new line was added (i.e. that one line having been moved), or should it show all the other lines in the file as having been deleted and re-added in a different place? Both are valid interpretations of the difference between the two snapshots, but of course a real-life diff tool will show the former, as that's what's helpful to the user.
- recursive 6y agoI'm glad that real-life diff tools work in ways that they deem to be useful. But they don't always produce the most sensible result. In fact different algorithms produce different diff visualizations for the same input. I'm just a little uncomfortable with a feature that uses diffs for purposes other than display for humans, since they don't seem to be consistently defined.
- vmchale 6y ago> How can two diffs ever be considered equivalent when they include a changed file that had different starting contents? Can they? In darcs, patches commute. It works nicely sometimes!
- aidos 6y agoI find it easiest to think in terms of diffs as patches. It’s just a bunch of search and update commands. Sure, there’s some hairiness around files moving or whatever, but for me, a cherry pick is just getting a patch file and applying it in the appropriate place. It doesn’t care about the file it’s being applied to, it just needs to find similar content in the file so it can apply itself.
- zokier 6y agoYeah, git cherry-pick is basically just git diff | git apply. And rebase is just cherry-picking many commits.
- aidos 6y agoExactly. And the other day I did exactly that with piping diff through apply so I had more control over conflicts I was hitting. Suspect all the years in svn just make it easier for me to think of it that way rather than some magical operation.
- MaxBarraclough 6y agoConceptually, a git commit is a snapshot. Simple as that. The user doesn't need to worry about how git manages its data internally. > I believe that Git becomes understandable if we peel back the curtain and look at how Git stores your repository data. This strikes me as misleading in the same way this StackOverflow answer [0] is misleading. In terms of how git stores data, commits are not always snapshots. Internally, git sometimes uses delta compression to reduce storage space. This is of no concern to the user, conceptually a commit is still a snapshot, but it's just not true that git naively stores each commit as a full snapshot. Peeling back the curtain isn't helpful to someone trying to understand the basic git model. Also, as the article makes no mention of delta compression or of 'packfiles', it seems to me it hasn't pulled back the curtain at all. [0] https://stackoverflow.com/a/8198276/ https://stackoverflow.com/a/8198276/
- stolee 6y agoThe delta compression in Git is about storing the file contents of an object as a diff against another object. This changes the literal size on-disk, but it doesn't change the logical unit. In fact, the delta chains used by Git for space compression have no direct relation to the object model DAG. From the perspective of a user using Git, these deltas are completely invisible. Edit: perhaps to help this point... If Git stores an object using a delta, that doesn't change the object ID of that object compared to storing it uncompressed.
- MaxBarraclough 6y agoRespectfully, I already made all of those points quite explicitly.
- tsimionescu 6y agoGit internal delta compression has nothing to do with why Git commits are most usefully thought of as diffs. All git operations start by computing diffs between commits, and then trying to apply those diffs before conceptually taking a snapshot. It is quite likely that the textual diff used in the operation and the internal diff produced by the delta compression are entirely different.
- mcc1ane 6y agoIs it normal to need that level of understanding of the inner machinery of a software to use it?
- contravariant 6y agoGiven that we're still figuring out new ways to use rocks, maybe?
- oblio 6y ago1. It is normal, as in, it's common. 2. Personally, I think this just shows how primitive our industry still is.
- phailhaus 6y agoI don't quite agree with the author that you need to understand that commits are implemented as snapshots, but yes, you need to understand quite a bit of how git works behind the scenes in order to use it effectively. This is because git tries to hide more than it should, which leads to confusion when things Go Wrong.
- deathanatos 6y agoI'd also say that when I've worked through people's confusion surrounding git (and other complex systems, it is because they are attempting to model the system as simpler than it actually has to be. They're not thinking of cases the system must handle, and in the absence of that (essential) complexity, they come up with an incorrect mental model that they believe works (as they're not aware of the counter-examples to it). This happens often. As another example, a former PM of mine used to assume that if our wearable recorded heart-rate data, it also had sweat data. That is, the wearable either had a lock, and recorded, or didn't. But sweat came from a different sensor than HR, and the two sensors could independently get or lose their signal. And so, sometimes one was null, sometimes the other, sometimes both, sometimes neither.
- phailhaus 6y ago> They're not thinking of cases the system must handle That's not their fault; git's CLI attempts to abstract away the details and presents a falsely simple view of the world with "checkout", "add", "commit", and "push". This works for a while, until it doesn't, and then you have to face the complexity of the graph all at once. If I was going to redesign its CLI, I'd make the graph as transparent as possible.
- phailhaus 6y agoWhy does thinking about commits as snapshots make cherry-pick and rebase easier to understand? I've always thought of and taught commits as diffs because git does a good job of abstracting away the distinction. EDIT: To be clear, I think this is one of the _few_ abstractions that git doesn't leak. It does a pretty bad job everywhere else.
- recursive 6y agoA snapshot is easy to understand. It's just the contents of a file at a point in time. Diffs can have different definitions, and I've never seen a complete rigorous one for whatever git uses, if anything. Loosely a "diff" is the changes between one version an another. But to understand what cherry-pick does, we (or maybe just I) need more than a loose definition. Using snapshots makes `rebase` trivial. The contents of a commit doesn't change. Just the link to its predecessor. Using a diff model, it requires all kinds of fancy changes.
- masklinn 6y ago> Using snapshots makes `rebase` trivial. The contents of a commit doesn't change. Just the link to its predecessor. It's the exact opposite. When you rebase, you want to apply the changes you're rebasing atop other existing changes. If you just copy over the snapshot, you break everything. Rebasing a snapshot basically smashes your history and is utterly useless. That would make a rebase-pull… implicitly revert every intermediate commit. Although AFAIK Git really uses the merge machinery to perform rebases: conceptually (I don't know if it does that technically) it merges each commit to rebase onto the rebase target, then copies over the commits without the old (rebased-from) parent.
- recursive 6y agoWarning: When I made this comment, I was under some false assumptions. Here's the original. --- A snapshot is the aggregate of all previous changes. I don't see what could be broken by copying the snapshot. I'm not familiar with rebase-pull, but I don't see why any revert would be involved. The snapshot before the rebase is exactly the same as the snapshot after the rebase. Only the predecessor is changed. The contents of the predecessor doesn't matter, since a snapshot contains the entire state of the tree.
- oblio 6y agoGit is the leakiest abstraction in the history of abstractions. Diffs are a "natural" object for version control yet got doesn't actually use them and as we can see in this article, multiple git commands leak this implementation detail.
- chubot 6y agoI'd argue that diffs in the presence of renames aren't natural, and git handles them better than other version control systems. That is, git is content-based and not named-based. Name-based systems explicitly track renames with metadata; git does not. git calculates renames dynamically based on content.
- masklinn 6y ago> I'd argue that diffs in the presence of renames aren't natural That is true, at least for the text-based diff(1) formats, though obviously nothing precludes extending the format, or even using something else entirely (aside from the risk of not being compatible with diff(1), but then you could always present diff(1) compatible diffs externally and use something richer internally, for instance). > git handles them better than other version control systems. But that is not. > That is, git is content-based and not named-based. Name-based systems explicitly track renames with metadata; git does not. git calculates renames dynamically based on content. And as a result regularly fucks up tracking renames, or plain refuses to do it without nonsensical workarounds if you copied or moved a file and modified it in the same commit. It also makes browsing histories, blaming, or tracking merges through renames incredibly expensive.
- chubot 6y agoThat could be all be true, but IMO git's handling is still better than the alternatives I've used (Perforce, mercurial, subversion, CVS). (Although honestly I never really have problems with it messing up. If you approximately separate renaming from editing, it seems to work very reliably for me.) There's also nothing stopping anyone from writing a git history browser that caches the calculation. The point is that it's ephemeral / derived state, not authoritative state. One problem with the name-based systems is that developers don't actually use the VCS rename operation. They just delete and add, and then you've lost the diff under every system I know of. Another problem is importing from other VCSes -- the metadata is often messed up subtlely. Git keeps it simple. And they can and have improved the algorithm over time without making your repo data obsolete / incompatible.
- dilap 6y agoThis is why I'm really excited about the potential of Pijul. In pijul, commits are diffs, and you avoid all of the rebase/cherry-picking craziness of Git. Still alpha software, so tons of roughs edges, but the potential is incredible. I think it'll be similar to the centralized -> distributed revolution that git ushered in.
- ldite 6y agoThere's nothing intrinsically superior about storing commits as diffs - subversion stores its commits as diffs (or at least, it did a decade or so ago,) and I haven't heard anyone enthusing about svn in a long time.
- fuzzybear3965 6y agoAre you sure about that? See "Why a new version control system?" at https://pijul.org/faq/ https://pijul.org/faq/ and the linked "badmerge" example: https://tahoe-lafs.org/~zooko/badmerge/simple.html https://tahoe-lafs.org/~zooko/badmerge/simple.html
- searealist 6y agoIf a project wants to hold up a bad merge as an example why they are better. They need to do better than just a list of letters. They need an actual example of real code.
- yoz-y 6y agoThere is a link to example real code in the first sentence there.
- searealist 6y agoThanks.
- Rexxar 6y agoReplace letters by line of codes and it will have the same result.
- nickthemagicman 6y agoI don't look at it as either. I simply look at it as a change in code at a location. The commit ID of that change in the history chain is calculated based on the commit ID before it and the one before that is calculated based on the one before that all the way down to the very first commit. (Similar conceptually to blockchain) So if you rebase or cherry pick or anything you will be building on top of a new commit ID so git will calculate a new commit ID for your new commit even though the code change is the same. It's pretty simple... if you understand that concept you can get 90% of the way there with git.
- gavman 6y agoFor a simple, easy to understand overview of git, nothing beats The Git Parable [1]. Every time I talk to someone starting out with Git I recommend they read it first. Once they understand Git through that lens, usually I find the rest falls into place. [1] https://tom.preston-werner.com/2009/05/19/the-git-parable.html https://tom.preston-werner.com/2009/05/19/the-git-parable.ht...
- 725686 6y agoHow about "Git For Ages 4 And Up" [1]? [1] https://www.youtube.com/watch?v=1ffBJ4sVUb4 https://www.youtube.com/watch?v=1ffBJ4sVUb4
- craftkiller 6y agoThis is my go-to video for anyone looking to learn git. It won't teach you the various commands on the command line but it shows you what is actually happening inside git when you perform various actions. It made me transition from "I know how to run these commands to operate git" to "I know what git is doing, so I can reason about the system and adapt to unusual circumstances". Now when someone makes a mistake in my company's git, I am the person people go to for help.
- avmich 6y agoStrictly speaking, it's a moot point. Snapshots and diffs are easily convertable to one another, which allows to present either way no matter the underlying structure. We can talk though about how the data is actually represented in storage. Here efficiency comes forward, and good compression algorithms will detect a small change for a big dataset. But that's not necessarily what would be present to the user - though it could be.
- jayd16 6y agoSnapshots and diffs are just a storage implementation detail, no? Can you not calculate one from the other?
- thaumasiotes 6y ago> Can you not calculate one from the other? Yes, you can. > Snapshots and diffs are just a storage implementation detail, no? If this is what you think, the headline is truly awful; git commits are stored as diffs where possible.
- fanf2 6y agoIt’s more complicated than that! Packfiles use delta compression to store repositories more compactly, but the deltas are not at all related to commits. The objects (blobs, trees, etc.) are sorted for similarity, completely ignoring the graph of commit history and completely ignoring filenames or directory structures, then delta compression is applied. This allows git’s storage to be smaller than version control systems that use a storage structure that preserves history or filenames.
- thaumasiotes 6y ago> Packfiles use delta compression to store repositories more compactly, but the deltas are not at all related to commits. The objects (blobs, trees, etc.) are sorted for similarity, completely ignoring the graph of commit history While I understand this point, I would make two further ones: - The storage is still diffs, and definitely not snapshots. It's just that the diffs of which a given commit consists do not all refer to the same "base". Contrasting git with a system in which commits are stored as one large diff (whereas git stores them as many small diffs) is something you can do, but it's not the same thing as contrasting git with a system in which commits are stored as snapshots. (Whereas git stores them as diffs.) - "Sorting for similarity" isn't a thing you can do; similarity does not induce a consistent ordering. > This allows git’s storage to be smaller than version control systems that use a storage structure that preserves history This is worded unfortunately; git wouldn't be a version control system at all if its storage structure didn't preserve history. It does, but it doesn't mix that concern with its concern for file contents.
- yodsanklai 6y agoGreat article. The first thing that I was told about git was that commits are snapshots. It is enough to understand the simple workflow: pull, commit -a, push. What confused me was everything that involves applying diffs (git stash apply, git rebase, git cherry-pick). The secret is that you need to think both in terms of snapshots, or diffs, depending on the context. Some git tutorials show you a few "magic" commands to get you started but it's best to really understand the git model as soon as possible. It's not that hard, and it's here to stay. We may still be using git in 30 years, just like we're still using bash and vim.
- espadrine 6y agoA fascinating bit of history is that the reason for this data structure was to explicitly distance git from BitKeeper. BitKeeper was used by many kernel developers, until one of them (the co-inventor of rsync, unsurprisingly) reverse-engineered its protocol, leading its proprietary owner to end its offer of a free license to Linus Torvalds and other developers. While searching for a replacement, the Linux community sparked two projects: Mercurial (which uses changesets, similar to BitKeeper) and Git (which uses Merkle trees). Git was created by Linus in the unexpected free time that this event created; and he mentioned being weary of Mercurial and having adopted this design for Git to remove any ambiguity that they were creating a BitKeeper clone. Incidentally, BitKeeper did demand that its customers not work on Mercurial[1]. [1]: https://web.archive.org/web/20070929115009/http://article.gmane.org/gmane.comp.version-control.mercurial.devel/3481 https://web.archive.org/web/20070929115009/http://article.gm...
- feoren 6y agoI wonder if this is why Mercurial is so much simpler and easier to understand than Git -- precisely because Git was forced to adopt a more "out there" approach. Personally I'm sad Git won out over Mercurial -- the latter is a much better technology from my experience.
- war1025 6y ago> Mercurial is so much simpler and easier to understand than Git I think it must just be a mindset thing. The way git works makes perfect sense to me and I don't know why you would do it any other way. But git was already gaining significant traction when I started using version control. I can see how having built your mental models around previous version control systems and then trying to superimpose that on git would cause dissonance. But my entire concept of version control is based on how git does it, so it seems perfectly natural.
- yoz-y 6y agoUnderlying technology aside, mercurial has (I think) quite objectively better cli interface. All flags are the same among commands, and all commands have their undo counterparts.
- z3t4 6y agoIve been thinking about creating a VCS that uses file system snapshots ...
- lilyball 6y agoEven though I knew everything about the internal structure already, reading this post was worth it for learning about `git range-diff`, which I'd never heard of before. This sounds exactly like a tool I've always wished Git had.
- da39a3ee 6y agoThe third sentence is wrong: > This is most apparent in commands that “rewrite history” such as git cherry-pick or git rebase. git cherry-pick does not rewrite history.
- karmakaze 6y agoDisregarding whether the title helps you or not, the content of the post is useful to put things into context if you're not already very familiar with git or find some things surprising. Commits are not 'just' the snapshots as the ancestor references and metadata matter. I for one think the title is a distinction without a difference (if you already accept that a git hash represents both the current set of exact files and ancestors) but the content is well presented for anyone.
- TrianguloY 6y agoWhen I need to explain git to someone for the first time I always start with the 'imagine you are copy/pasting your project folder, and you have multiple folders tagged with some details. Each folder is a commit'. People understand very easily how to keep copies of their work at a given time by simply coping the full folder (probably we all did this before using git). So yes, a commit is just a snapshot, a copy, it is simply saved in a much more efficient way. The issue is that some advanced (and not so advanced) commands, like a merge, aren't easily explainable with this metaphor, and that's when the 'commits are also diffs', or better 'commits can be seen as diffs' is needed.
- rualca 6y ago> So yes, a commit is just a snapshot, a copy, it is simply saved in a much more efficient way. Not really. Although it might be helpful to explain to someone entirely new to git that a commit marks a revision, and a revision is kind of a copy of a project folder, that mental model has no bearing to what a revision control does or helps you accomplish. I mean, as soon as you talk about diffs and branches and cherry picks and merges then thinking about copies of a folder spread around does not explain what's happening. And what's the point of git if not to track and manage diffs?
- globular-toast 6y ago> that mental model has no bearing to what a revision control does or helps you accomplish. It's exactly what revision control does and helps you to accomplish. What do you think revision control is for if not that?
- globular-toast 6y agoYeah. That's a perfectly valid form of version control. You could tell them to imagine renaming the directory each time with an incrementing number. That's the version number, hence version control. Merge can be explained too. Say you send the folder to your friend and that night you both make a new version. If you want to consolidate your changes you have to merge them together. I don't see why this requires commits to be seen as diffs. Rebase is a bit more tricky. Maybe that's what you were thinking of?
- globular-toast 6y agoA surprising number of people think that git commits are diffs. To fully understand git you have to understand the DAG and that absolutely does not contain diffs! Great article.
- gnufx 6y agoAll this does seem to help make the argument for simple patch-based systems, i.e. Darcs and Pijul, in common with much discussion I see about git. (Their patches aren't just deltas, of course.)
- mproud 6y agoshapshots?
- deleted 6y ago[deleted]
- develop7 6y agoFor 12439th Time, Git Is Simple And Beautiful! Can you See It Now? Another attempt to explain Git implementation details & lingo, because rewriting Git end user docs to not refer those is borderline insurmountable.