Day 3 - Deep diving into Git

Hello! So today I learned how exactly git works. In this article I will share what I learn, from where and my next goal.

When I first studied Git few years ago, I was fascinated by how it works, what are snapshots, how branches are managed, how merge is performed. And most importantly, Can I create my own Git?

Here is what I learned today!

Repository: A collection of commits.

Commit: A snapshot of your working tree at some point in time.

Working tree: A directory on your filesystem which has a repository associated with it. Branch: It is just a name for a commit.

Tag: It is same as branch but they can have their own descriptions.

HEAD: Used by repository to define what is currently checked out.

Git and file system are similar but different in few aspects. Git uses blob and file system uses files. Both differ in the aspect that a blob stores no metadata about its content. While a file does.

git hash-object <filename> : returns Hashid of a given file, same file, same hash always. Doesn't depend on the system. Depends only on the content of the file.

git cat-file -t <hashId> : returns the type of hash. It can be either commit, tree or blob

git cat-file <type> <hashId>: returns the content of the hash. For example git cat-file blob <hash> will return the contents of a file.

git ls-tree <hashId> : returns the content of the tree.

.git/objects stores all the object in a repository

git ls-files --stage : returns all the objects that exists even though there is no commit

.git/index : holds the references of the blobs and trees that make up the current index.

HERE

git write-tree : creates a new tree containing the objects currently in the the .git/index

echo <commit_message> | git commit-tree <hash> : takes a tree's hash id and makes a commit object to hold it.

echo <hash> > .git/refs/heads/master : this tells git to refer a commit as the new head of a branch.

safer way: git update-ref refs/heads/master <hash>

git symbolic-ref HEAD refs/heads/master : this associates HEAD symbolically with the master branch.

EREH

git log : works now. FROM HERE TO EREH

Source: Git from Bottom Up

Next step is to read Write Yourself a Git! and implement it in C.

0
Subscribe to my newsletter

Read articles from Utkarsh Swaroop Shrivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Utkarsh Swaroop Shrivastava
Utkarsh Swaroop Shrivastava