Thursday, October 8, 2015

New to Git

This is how I understand Git

Git is used to store files, track history, share files, collaborate work.

How Git works?
Git is like a key value data store. Git stores files and folders as objects.

The objects are named using they SHA-1 hash - 40 character checksum value of the content. The objects are stored in .git/objects folder. The object is placed in folder with name as the first two characters and file name as the remaining 38 characters. The object contains header (type & size) and body.

There are 3 types of objects
1. Blob - File
Blob contains the content of the file

2. Tree - Folder
Tree contains reference to other trees and/or blobs

3. Commit - Snapshot of all the folders and files at some point in time. The object contains the info about the root tree (top most tree)
  • tree
  • parent (at least one parent, first commit has no parent, merge commit has two parents)
  • author
  • committer
4. Tag

Representation of how Git stores files and directories as objects

Representation of how Git tracks history

    You may not like to remember the 40 char SHA-1. So Git provides a mechanism to point to those commits. The HEAD is the reference which points to the current commit and moves along with you. You can think of references as labels to commit or other references. Branch is another type of reference head. By default, when you create a git folder (command: git init), you will be on master branch. Before you commit any file/folder, the master reference will not be created. When you create a branch BRANCHNAME, a new reference (.git/refs/heads/BRANCHNAME) is created with content pointing to the current commit. The HEAD usually points to a branch which in turn points to a commit. But some times HEAD can point directly to a commit (detached head). When HEAD is pointing to a reference head (branch), any new commits will make the branch reference point to the new commit. This cause the HEAD also point to the new commit due to transitivity. But in detached HEAD mode, when a commit is made, the HEAD points to the new commit and the branch is not updated. Now, if you want to bookmark a commit for future reference, you need to store the SHA-1 of that commit. This is tedious. Git comes to your rescue. It provides tags which are also references which are immutable. Once you tag a commit with a name, that name can be used to reference the commit any time in the future. The tag will always point to that commit. It cannot be modified.

    More on Git coming soon.


    No comments: