What is a git "Snapshot"?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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.

Solution 7 - Git

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".

Solution 8 - Git

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

Solution 9 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjensView Question on Stackoverflow
Solution 1 - GitphilippeView Answer on Stackoverflow
Solution 2 - GitFemarefView Answer on Stackoverflow
Solution 3 - GitlinjiejunView Answer on Stackoverflow
Solution 4 - GitxgedView Answer on Stackoverflow
Solution 5 - GitMarcelo CantosView Answer on Stackoverflow
Solution 6 - Gitbrent777View Answer on Stackoverflow
Solution 7 - GitG_VView Answer on Stackoverflow
Solution 8 - GitDotistaView Answer on Stackoverflow
Solution 9 - GitLtf4anView Answer on Stackoverflow