by BehindJava

What is Snapshot in Git and the purpose of Snapshot

Home » interview » What is Snapshot in Git and the purpose of Snapshot

In this tutorial, we are going to learn about Snapshot in Git which is a backup copy for each version git stores in repository. It represents some data of particular time so that we can get data at particular time by taking that snapshot. This snapshot will be taken in Staging area which is present between Git workspace and Git local repository.

“Snapshot” in Git

It is a backup copy for each version git stores in repository.

  • Snapshot is an incremental backup copy (only backup for new changes).
  • Snapshot represents some data of particular time so that, we can get data of particular time by taking that particular snapshot.
  • This snapshot will be taken in Staging area in Git which is present between Git workspace and Git local repository.

Steps to Initilise a folder in git

  1. To initialise a folder as a git repository.

    git init 
    • This will create a hidden folder called .git where it stores all the configurations necessary for git.
  2. To send a file into stagging area.

    git add filename 
    • To send multiple files into stagging area.

      git add file1 file2 file3 
    • To send all the files and foders and sub folders into stagging area.

      git add . 
    • Note: . represents present working directory.
  3. To send the files back from staggig area to working directory

    git rm --cached filename 
    (or) 
    git reset filename 
  4. To send the files from stagging area to local repository.

    git commit -m "some message" 
  5. To see the status of unsteacked and stagged files.

    git stauts 
  6. To see the commits done on local repository.

    git log 
    • To see the above output in once line format.

      git log --oneline 
  7. Delete untracked files:

    git clean -n <filename> 
    Git clean -f : to confirm  

gitignore

This is a special configuration file which is used to hide the private files info.Any file whose name is mentioned in .gitignore will no longer be accessed by git.

  1. Create few files.

    touch f1 f2 f3 f4 f5 
  2. Check the status of git.

    git status 
    • This will show the above 5 files as untracked files.
  3. Create .gitignore and store the above 5 file names in it.

    cat > .gitignore 
    f1 
    f2 
    f3 
    f4 
    f5 
    • To come out of cat command press ctrl+d.
  4. Check the status of git.

    git status 
    • This will only show .gitignore as untracked and f1-f5 are no longer accessable by git.