DEV Community

Cover image for How Git works?
5 Mins Learn
5 Mins Learn

Posted on • Updated on • Originally published at 5minslearn.Medium

How Git works?

Please read my previous article (What is Git?) before > Please read my previous article (What is Git?) before reading this article

https://5minslearn.medium.com/what-is-git-4bf01d929524

How Git Works?

Git has three main states that your files can reside in.

  1. Working Directory
  2. Staging Area
  3. 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 Other VCS stores file changes?


How Git 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

Subscribe to our newsletter to receive more such insightful articles that get delivered straight to your inbox.

Reference

Git - What is Git?

https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F

Top comments (0)