How Git works?

How Git Works?
  1. Staging Area
  2. Local repo (Commited)

Working Directory

  1. Working Directory is a state where you have changed the file but have not committed it to your database yet
  2. This state may contain tracked and untracked files
  3. Untracked files will not be tracked in git unless you commit the file

Staging Area

  1. This state means that you have marked a modified or untracked file in its current version to go into your next commit snapshot
  2. Staging area can contain multiple files
  3. The technical name of this state is “index”, but it’s referred as staging area everywhere

Local repo (Commit)

  1. Commit is a state where your data is safely stored in your local database
  2. Git stores the metadata and object database for your project in this state
  3. When you make a commit, git takes a snapshot of your code into `.git` directory

Basic git workflow

  1. Create or modify a file in your working directory (aka working tree)
  2. Selectively stage just the changes you want to be part of your next commit, which adds only those changes to the staging area
  3. Make a commit (aka checkin), which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory
Basic git workflow

How Git store and retrieves data

Snapshots, Not Differences

  1. The major difference between Git and any other VCS (Subversion, Perforce, Bazaar, etc. ) is the way Git thinks about its data
  2. Most VCS work by delta-based version control (They store as a set of files and the changes made to each file over time)
  3. Git thinks its data as a stream of snapshots (Like a series of snapshots of a miniature filesystem)
  4. Git stores a reference to that snapshot every time you make a commit
  5. This makes Git more like a mini filesystem with some incredibly powerful tools built on top of it, rather than simply a VCS
How Other VCS stores file changes?
How Git Stores file changes?

Nearly Every Operation Is Local

  1. Unlike most CVCS systems, most operations in Git need only local files and resources to operate
  2. All operations seem almost instantaneous, as the entire history of the project is right there on your local disk
  3. You can browse the history of the project, play with commits, make a new commit (without network connection) and finally sync your changes when you’re connected back to internet which is not possible with Perforce, Subversion, CVS, etc.

Git has integrity

  1. Everything in Git is check summed before it is stored and is then referred to by that checksum
  2. Git uses SHA-1 hash mechanism which is a 40 character string composed of hexadecimal characters (0–9 and a–f)
  3. A SHA-1 hash example 24b9da6552252987aa493b52f8696cd6d3b00373
  4. Git stores everything in its database by the hash value of its contents

Git only adds data

  1. Most actions in Git result in adding data to the Git database
  2. Once you commit a snapshot into Git, it is very difficult to lose

Reference

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
5 Mins Learn

5 Mins Learn

Learn any concept under 5 minutes.