Skip to main content

Command Palette

Search for a command to run...

Inside Git: working and .git Folder

Updated
2 min readView as Markdown

Internal Working —

Let’s go through the internal working of git (how git does it) and how we can verify it using few commands

  • From the .git directory, Git always starts from HEAD.

    Here, there’s reference to the current branch refs/heads/main (so main is taken as the branch)

  • Next, git goes to .git/refs/heads and looks for the file that matches the branch name it got from HEAD.

    Here, it reads commit hash stored in main which is of the HEAD commit of the repository.

    Now, onto the actual commit data

  • inside objects (.git/objects), the info of these commits is stored, like so:

    Further from this parent commit, git moves to its previous commit and so on.

    This is the internal working of git and how entire commit history connects.

  • Git stores commits as snapshots, while reusing unchanged data, so only new or modified content takes up space. (only changes are kept)

  • We can verify what Git is doing using these commands:

  •       cat .git/HEAD
          cat .git/refs/heads/main
          git cat-file -p <commit-hash>
    

.git directory —

.git is special directory created when we run the command git init.

It is hidden by default to discourage manual changes inside it. Without the .git directory, the project is no longer a Git repository.

We do ls -a to view it. It holds all records, history and metadata related to the repository.

how git commands use .git

  • git branch
    reads .git/HEAD and then follows refs/heads/main to determine the current branch.

  • git add
    Changes are moved to the staging area, making them ready to be committed.

  • git commit
    creates a new commit object, stores staged changes inside .git, and generates a unique hash for the commit. Each hash maintains integrity and keeps reference to its parent commit.