89

The official Git doc says:

$ git diff test

This will show you what is different between your current working directory and the snapshot on the 'test' branch

As a newbie this is very confusing. I've never heard of the term snapshot. Do they mean the "HEAD" of the "test" branch?

Community
  • 1
  • 1
jens
  • 2,319
  • 4
  • 18
  • 11
  • 13
    You might also appreciate [The Git Parable](http://tom.preston-werner.com/2009/05/19/the-git-parable.html), which builds up the idea of Git in story form, with one of the core ideas being snapshots. – Cascabel Feb 11 '11 at 00:23
  • 1
    Thanks I had the same question. Looking at the Git Parable, and at https://git-scm.com/book/en/v2/Getting-Started-Git-Basics , I am understanding a "snapshot" as an entire duplicate of every changed file in a directory, plus pointers to all the unchanged files in that same directory. Is that correct? – ouonomos Nov 18 '15 at 05:37

9 Answers9

50

The term snapshot is used in the git reference site as well

It is the replacement term for "Revision". In other version control systems, changes to individual files are tracked and refered to as revisions, but with git you are tracking the entire workspace, so they use the term snapshot to denote the difference.

From http://gitref.org/index.html

Instead of writing a tool that versions each file individually, like Subversion, we would probably write one that makes it easier to store snapshots of our project without having to copy the whole directory each time.

This is essentially what Git is. 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. Then most of the commands work with those manifests to see how they differ or pull content out of them, etc.

If you think about Git as a tool for storing and comparing and merging snapshots of your project, it may be easier to understand what is going on and how to do things properly.

Community
  • 1
  • 1
philippe
  • 546
  • 1
  • 3
  • 3
  • i'd also like to point out that you can get a 'snapshot' of an individual file in git by doing `git checkout ` as listed in the git checkout examples https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html#_examples – g19fanatic Sep 11 '13 at 14:56
  • 1
    @Jens: While you can't split the checkmark, you can re-assign if you think other answer is best than the currently accepted answer. But, yes more UPVOTES, mean the community felt the answer is so good/valueable. Also, that doesn't mean that have to be accepted. Accepted Answer is something upto the OP. – smRaj Nov 08 '13 at 04:31
40

A snapshot is the state of something (e.g. a folder) at a specific point in time. In this case, snapshot means the current content of the test branch, this doesn't have to be the head revision.

SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • 3
    @jens: Snapshot is being used here as a layman's synonym for a commit. A commit object is essentially metadata (author, date, message) and a snapshot of the contents of your work tree (represented as a tree object). See [The Git Object Model](http://book.git-scm.com/1_the_git_object_model.html) for more information! – Cascabel Feb 11 '11 at 00:17
  • I can't comment directly on the git case, but in the case of SVN you could have a couple of changes (uncommited, meaning only on your harddrive) in a branch and the trunk version in another folder; you could diff those against each other. I suppose that concept exists with git as well. – Femaref Feb 11 '11 at 00:17
  • @jens: The other piece is that a branch is merely a pointer to a commit, so when you say `git diff test` you really mean "diff with the commit pointed to by the `test` branch". – Cascabel Feb 11 '11 at 00:17
30

In order to explain the term snapshot clear. Please allow me to introduce the other two things

  1. git loose object format
  2. git Packfiles

Assume we have a file called "a.txt" which contents are ten 'a' letters under git control. After we commit this file, it will create three folders under .git/objects path and each folder has one file. Each file is S.N.A.P.S.H.O.T.

a.txt file under git control which has been committed three folders created by git after committed

Each folder has one file

enter image description here

Now we edit the a.txt file to see what happens

We changed the first letter 'a' to letter 'b'

enter image description here-->> enter image description here

Then committed!

Git created other three new files under three new folders

enter image description here

These three new files are also S.N.A.P.S.H.O.Ts

enter image description here

Every time we do COMMIT git will save snapshots to disk instead of the delta between the new version to the old version of the same file. Even if we just changed one letter of one file, git will save the whole file as a snapshot.

This also called the loose object format.

In this case, git will cost more disk space than the other vcs(such as subversion) which saves delta between the new version to the old version of the same file. But the benefits of using snapshot shorten the time at commit stage.

But the brilliant git will do another jobgit gc after this from time to time which created PACKFILES and removes the snapshot which contents are similar to shrink the size of itself. After these workgit gc, the disk cost by git will just like the other VCS which uses delta ways.

Through snapshot and git gc. Git will faster than other VCS which use delta ways at commit stage and costed the disk size just be similar to the other VCS that use delta ways.

Git finds a balanced way between the performance and disk space cost.

GIT is best

The packfiles is under .git/objects/pack

You can see it after execute "git gc" command by yourself.

linjiejun
  • 1,468
  • 15
  • 22
  • 1
    This clarifies what a so called snapshot is really made of. Yet, is says that a commit will create three folders, each one with a file in it; and that "Each file is S.N.A.P.S.H.O.T.". But each snapshot is a single commit, a saved state of all the repository. That snapshot is made of several files (three in this case for the files), so it'd be more appropriate to say that here the snapshot is made with those three files. – Martin Apr 21 '19 at 19:16
  • 1
    For those not familiar with hash values or not seeing the relation between the folders pics and the snapshots pics at first glance: hash values are sometimes referred by the first numbers. In Git, you can refer directly to a commit by using its hash instead of a branch name. Here, folder named 67 (first of last new ones) has the file with the large number 67a311... depicted in the following pic, which is the hash... So each 3 folders names correspond to the first 3 files of the 4 in the following pic. – Martin Apr 21 '19 at 19:20
  • And continuing with what a snapshot is; apart from the files that contain files content (blobs in Git), it also includes 2 other objects: a tree that lists the contents of the directory and relates them with the blobs, and one commit with the pointer to the previous tree(s) and all the commit metadata. All together makes the 'snapshot' (the commit), which it is of all the repository, not of a particular file. – Martin Apr 21 '19 at 19:33
  • @Martin : Those files making up a snapshot seems very small. While the content could be very large. How those small files can store all these big content. Just hashing the content? – Wason Nov 16 '21 at 08:14
13

Snapshot is to a repository as screenshot is to a video.

It's the content (files and folders) of a repository at some point in time.

When you commit, you save your repository's current working directory as a new snapshot (commit = snapshot + metadata). Your Git repository consists of series of snapshots (commits), other VCS consists of series of diffs instead.

xged
  • 1,207
  • 1
  • 14
  • 20
3

Firstly, that's not the official git documentation. It's a community-authored book. While it is probably fairly authoritative, it isn't gospel.

AFAIK, "snapshot" doesn't have any formal meaning in git. Certainly the git diff manpage doesn't mention it. In the given context, it is probably an informal reference to how the "test" branch is being used in the examples within the book, i.e., as a snapshot of ongoing work, for testing purposes.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 5
    the git-stash man page mentions `For quickly making a snapshot, you can omit both "save" and ` – Mike Dec 13 '15 at 13:20
1

My understanding is that a snapshot in general is just the "entity" that git uses to store its data. As opposed to storing its data as a series of "deltas" / changesets like SVN does, for example, each commit that you do to git creates a "commit object" that references a snapshot of what the code looked like at that point in time.

So as @Femaref says, it is the state of the code at a specific time and does not necessarily mean it is the head of the test branch but could be in the example you saw.

brent777
  • 3,369
  • 1
  • 26
  • 35
1

Based on the other definitions posted:

A snapshot is a representation of the current state of your tracked files in the form of a manifest, which can be compared with other manifests to see where the differences are.

Git only tracks the differences between manifests from the first moment it was tracked. If a file has not been changed since the very first snapshot, all subsequent snapshots will refer to the very first snapshot. If the file has been changed, all subsequent snapshots will refer to the snapshot that has the most recent changes. What git stores is effectively a chained history of snapshots from the last to the very first. A branch is basically a split in the timeline, allowing for an alternate historical chain of snapshots from a specific snapshot on the main chain.

Branches are usually intended for features and such that may at some point be merged into the main branch. If there's no intention merge, but rather a divergence from the original project in the form of a new and entirely separate copy of the project with its own history, it's often called a hard fork, referring to "a fork in the road".

G_V
  • 2,396
  • 29
  • 44
0

Branch which is not a definitive release version, likely work in progress

Dotista
  • 404
  • 3
  • 12
-1

Following are some of the contexts in which the term snapshot is used:

  1. A new commit is created by taking a snapshot of the Index.
  2. A commit is a snapshot of the repo at a given time.
Ltf4an
  • 787
  • 5
  • 18
  • 2
    you used the term to answer your question, but the question directly asking what is a git Snapshot. – Umar Ata Aug 05 '17 at 08:37