Skip to main content

Command Palette

Search for a command to run...

Inside Git: How It Works and the Role of the .git Folder

Published
3 min read
  1. How Git Works Internally

    if we install git in our local system, it also tracks the changes in code in our local machine.

    as we code not only one day, we usually work on any project for no of days, in that scenario the git helps to track the changes and modifications in our code on daily basis.

  2. Understanding the .git Folder

    when we .git init in any repository, the hidden .git folder is created in out project folder.

    There are several hidden folder inside the .git folder, which help us to track the changes in the code. There is head folder which contains the current branch address. There is ref/head/main, which shows the current hash id.

  3. Git objects in a simple manner

    Consider Git as content-addressable storage system that saves snapshots, not files over time.

    The 4 Core Git Objects are:

    1 ) Blob - The content => means what is inside the file.

    • stores the actual content of a file

    • does not store filename, folder or permissions

    • same content → same blob (even if filename changes)

2 ) Tree - The structure => means how files and folder are arranged.

- Stores :

  • File names

  • Folder names

  • Links to blobs (files)

  • Links to other tress (subfolders)

3 ) Commit - the snapshot => means how the project look like at this time.

commit = saved project state

- Stores :

  • pointer to root tree

  • author info

  • commit message

  • timestamp

  • pointer to parent commit

4 ) Tag - the label => means which is the important commit.

  • used to mark important commits

  • example v1.0, v2.0

  • lightweight or annotated (with message)

  1. Show what happens internally during git add and git commit

    git add <filename> will take the file into the staging area that means changes and code are ready to save or commit.

    git commit -m “message” with this command, the changes are saved with some message for information and identify the changes easily.

  2. Explain how Git uses hashes to ensure integrity

    Everything in Git is identified by a cryptographic hash of its content.

    A hash is like a digital fingerprint of data.

    • input : file content /object content

    • output : a fixed-length string (SHA-1 or SHA-256)

if a single character changes, the hash changes completely.

Git stores 4 objects, and each object is named by its hash

1) Blob

2) Tress

3) Commit

4) Tag

How integrity is guaranteed :

— Hash depends on content, it includes

  • object type (blob/tree/commit)

  • object size

  • actual content

— Objects are linked by hashes, commit stores tree hash, tree stores blob hashes and commit also stores parent commit hash.

— any corruption is instantly detectable

  • a file changes on a disk

  • a blob is corrupted

  • a commit is tampered with

Then :

  • The stored hash is not similar with recomputed hash

  • Git detects it immediately

Internal flow of git add and git commit

flow diagram of head → commit → blob