Skip to main content

Command Palette

Search for a command to run...

An In-depth Look at - How Git Works ?

Updated
4 min read
An In-depth Look at - How Git Works ?
T

B.Tech student at NIT Jalandhar, currently learning web development and actively upskilling in frontend fundamentals. Exploring HTML, browser behavior, and how the web works behind the scenes, with a focus on building strong basics. I believe in consistent learning, understanding systems deeply, and improving step by step. Documenting my journey, building projects, and growing toward tech-driven roles through hands-on practice 🚀

Git is a distributed version control system that helps developers track changes in their codebase. Here's a look at what happens inside a Git repository and how the .git folder is structured:

  1. Initialization: When you initialize a Git repository using git init, a hidden .git directory is created in the root of your project. This directory contains all the metadata and object files that Git uses to manage the repository.

  2. .git Directory Structure:

    • HEAD: A reference to the current branch.

    • config: Configuration settings for the repository.

    • description: A brief description of the repository (used by Gitweb).

    • hooks/: Scripts that Git can execute at certain points in the workflow (e.g., pre-commit, post-commit).

    • info/: Contains the exclude file, which is similar to .gitignore but specific to the repository.

    • objects/: Stores all the content (blobs, trees, commits) in a compressed format.

    • refs/: Contains pointers to commit objects for branches and tags.

  3. Objects: Git stores content as objects in the .git/objects directory. There are three main types of objects:

    • Blobs: Store file data.

    • Trees: Represent directories and contain pointers to blobs and other trees.

    • Commits: Store metadata about changes, including pointers to tree objects and parent commits.

  4. Index: Also known as the staging area, it holds changes that are ready to be committed.

  5. Branches and Tags: Managed within the refs/heads and refs/tags directories, respectively, these are pointers to specific commits.


GIT ADD , GIT COMMIT : What Really Happening ?

When you use git add, you are adding changes from your working directory to the staging area (also known as the index). This means that the changes you have made to your files are being prepared to be included in the next commit. The git add command does not actually record the changes in the repository; it simply stages them for the next commit.

When you use git commit, you are taking the changes that have been staged and creating a new commit in the repository. This commit is a snapshot of your project at that point in time. It includes all the changes that were staged with git add. The commit also records metadata such as the author, date, and a commit message that describes the changes. Once committed, these changes are stored in the .git/objects directory as a new commit object, which points to the tree object representing the state of the project at that time.

(To build a mental model of Git, think of it as a system that tracks snapshots of your project over time. Each snapshot is a commit, and each commit is a point in the project's history, linked to previous points by hashes. This model helps you understand that Git is not just about commands, but about managing a series of states and changes in a secure and reliable way.)


HASHES IN GIT :

Git uses hashes, specifically SHA-1 hashes, to ensure the integrity and uniqueness of the data it manages. Each object in Git, whether it's a blob, tree, or commit, is identified by a unique SHA-1 hash. This hash is generated based on the content of the object, ensuring that even a small change in the content will result in a completely different hash. This mechanism guarantees that the data has not been altered or corrupted.

In Git, hash integrity is like a digital fingerprint for each piece of data. Every file, directory, and commit in a Git repository is assigned a unique identifier called a SHA-1 hash. This hash is generated based on the content of the data, so even a tiny change in the content will produce a completely different hash. This ensures that the data hasn't been altered or corrupted.

Think of it like a seal on a letter. If the seal is broken or different, you know the letter has been tampered with. Similarly, in Git, if the hash changes unexpectedly, it indicates that the data has been modified. This mechanism helps Git maintain a secure and reliable history of changes, ensuring that the project's history is accurate and trustworthy.


SUMMARY :

Git is a distributed version control system that tracks changes in a codebase. It operates by creating a hidden .git directory in the project's root, which contains metadata and object files. Key components include:

  1. Initialization: git init creates the .git directory.

  2. .git Directory Structure: Contains files like HEAD, config, description, and directories like hooks/, info/, objects/, and refs/.

  3. Objects: Git stores data as blobs, trees, and commits in the .git/objects directory.

  4. Index: The staging area for changes ready to be committed.

  5. Branches and Tags: Managed in refs/heads and refs/tags, pointing to specific commits.

When using git add, changes are staged for the next commit. git commit creates a snapshot of the project, storing it as a commit object. Git uses SHA-1 hashes to ensure data integrity, with each object identified by a unique hash. This system ensures a secure and reliable history of changes.