Skip to main content

Command Palette

Search for a command to run...

The .git Folder Explained: How Git Functions

Updated
6 min read
The .git Folder Explained: How Git Functions

How Git Works Internally

If you are referring this to GitHub, then you don’t know git

Let’s start from the beginning of Git. So firstly, have you ever thought about how git works and how it maintains your codebase's history?

When you write this in your terminal/command line

$ git init

And boom, a magic happened? No, a Marvelous Work of engineering happened behind the scenes. Git just initialized/created a hidden folder called .git. If you are wondering why you have never seen this folder because Git keeps it hidden from you. All the things happening inside the .git folder, like keeping the history and whatever things are changing, are important, so they don’t want somebody to come and mess with the folder. Now, let’s go forward.


$ git add "index.js"//This command is for only specific file
$ git add . //This command is for whole folder

Git just took a snapshot of your whole project folder/file, and now Git can keep track of how the folder was looking and if any changes happen the it knows which folder or file is changed because git knows the previous version of your folder, but git not track word by word it track line if git track’s every word then it will be not memory and space efficient so it track line’s we will be discussing more about this in upcoming section first let’s dive Inside the .git folder.

$ git commit -m "Here the message related to your changes" //Here you can write the message whatever you just changed.

After this command, the files are moved to staging, which means now the change will move to the .git folder. Till now, this was only tracking, but not moved to the .git folder. After this command, it also moved. Git stores the commit in a hash format. Again, if you make any changes in the committed file, it makes a new hash and takes the reference from the last commit. and it compares with the last reference, so the changes can be tracked

Understanding the .git Folder

.Git folder structure

# .git Directory Structure

```text
.git/
├── HEAD                # Points to the current branch
├── config              # Repository configuration
├── description         # Repo description (used by GitWeb)
├── index               # Staging area
├── objects/            # Git object database
│   ├── info/
│   └── pack/
├── refs/               # References (branches, tags)
│   ├── heads/
│   └── tags/
├── hooks/              # Client-side hook scripts
│   └── *.sample
├── info/
│   └── exclude         # Local ignore rules
└── logs/               # Reference logs
    ├── HEAD
    └── refs/
        └── heads/

Inside the .git folder are all the files that are essential to keeping the repository up to date. Yes, the word repository in the world of Git is the folder of yours called repository

  1. Inside the .git folder, a folder called /objects that keeps Git’s database

  2. refs/ keeps the branch and tag pointers

  3. HEAD Keep your current status

  4. index is the staging area

Git Objects: Blob, Tree, Commit

What are these weird names, right? Let’s understand these

This is built on a content-addressable filesystem, where blob, tree and commit work together to store our project history as snapshots

  1. Blob (binary large object) - This stores raw content of of files it is a binary searies of data and contains no metadata like where its stored when it stored and no filenames with that each blob idetentified by a unique SHA-1 hash generated from its content, if other files and your current file has same content then that also will be considered as a blob object which saves and optimize the space and duplication in the memory.

  2. Tree -Git working tree represents the recent or current snapshot of your repository , keeping all information of your files and directories that are tracked by git. This Tree is storing your metadata and object database so it can handle multiple directories from the same repository it’s like you are working on a new bug or new feature without affecting the main branch yes you can do that if you dont sure or you dont want to affect that then you can create a brance and it looks like tree diagrame thats why they named is as tree. And after you merge your new features to the main command, it knows where git has to merge. And it's decided by you.

Commit - as we were looking at this before, let me explain to you how this works when you stage your git. After that, the git commit comes, it takes your desired files and moves to the git local database, or inside the .git folder, it includes metadata and more, like A unique SHA-1 hash identifier and the author’s name and email, with the date and time. Lastly, it is described with your commit messages

How Git Tracks Changes

As we already know, Git takes a snapshot of your file/folder. Git ensures data integrity using cryptographic hashes. Before, it was SHA-1; now it’s transitioning to SHA-256. Anyways, it creates unique fingerprints for every piece of word lines, these have been called objects, as you can see in the file structure above here, it’s going to store .git/objects

Also, git does some metadata caching so that every tracked file, the index keeps metadata such as file size and last modification time from the filesystem. After this, when you run commands like git status, git does a fast check by comparing the current directory metadata with the cached data inside the index.

After the git tracks the changes, if the metadata does not match, it knows the changes happened, and git reads the file content and calculates the SHA-1 to cross-check if the content has actually changed. This makes git efficient and fast, so it doesn't have to do the hashing every time for every file.

This image Internal flow of git add and git commit

Git commands with explainable meanings

Repository setup

git init
→ git initialize_repository
→ “Start tracking this folder with Git”
git clone <repo>
→ git copy_repository
→ “Download an existing Git project with full history”

Tracking & staging

git status
→ git show_current_state
→ “What changed? What is staged? What is not?”
git add file.txt
→ git stage_file_for_commit
→ “I want this file included in the next snapshot”
git add .
→ git stage_entire_folder
→ “Stage all changed files in this project”
git restore file.txt
→ git discard_local_changes
→ “Undo changes in this file (not staged)”

git COMMAND --help

If you are new to git, please try this command and start reading through the first paragraphs to see if the explanation fits our mental models.

More from this blog

Prakash Jha Blogs

23 posts

Trying to write Blogs on day-to-day applications or technology from a different perspective, you never thought of how amazing those technologies are.

The git folder explained