Wednesday, September 7, 2016

How do you explain the basics of Git in simple words?

While working with code one often finds the strange words used by git version control system e.g. branch, tree, clone, commit etc. Most of the git tutorials and help available is difficult to understand and easily goes over head. This article will explain git in simple words and diagrams that I used myself to understand how git works.

Blob





You will not see the word blob much while working with Git but it is the most basic git data storage. Git stores just the contents of the files for each change which is called Blob in the git terminology.

Hash (e.g. 457AEF93)



The numbers you see are SHA1 hash of contents which identify every object in git, be it a commit, tree, or blob. It is 40 character but only first few characters (10 on Github) are enough to uniquely identify anything. On github you will see a hash with every commit.

Tree (OR Folder OR Directory)




Tree in Git terminology is like Folders or Directories. There is always a Tree on top of your git repository which knows what files (blobs) and folders (trees) it contains as seen in above diagram.
Each tree has a hash and contents info
More details Git Object: tree | GitGuys.com

Commit (OR Snapshot)

Following is an over-simplified diagram of what a commit represents.


A more detailed diagram of commit, tree and blob structure.

Commits can be thought of as snapshots: they know what the trees looked like at one point in time. Each commit has a hash, committer info, time and a comment. If you add edit a file and commit it again, git will save the updated file with all the changes as a new blob and this new commit will contain this whole new blob. Now your git repository have both versions of this file.
More Details Git Object: Git Commit - GitGuys

Check in = Doing a Commit

Check in is simply another name for doing a commit in the repository.

Commit History



If you make some changes and commit again, the next commit stores a pointer to the commit that came immediately before it. So the commits “flow” in one direction only. This flow of commits can be divide into multiple branches.
By default all commits go on master branch, but you can always create more branches. Commits are connect like a linked list where each new node points to previous node unlike simple link list. This gives us the power to create multiple nodes pointing to same previous commit. e.g. in above diagram 3rd and 4th commit both point to 2nd commit hence creating two branches.

Branch (OR Save as)



A branch is a series of commits that can be divided in further branches. Default branch name is master. A branch is actually only a move-able pointer. Initially you’re given a master branch that points to the last commit you made. Every time you commit, it moves forward automatically pointing to the latest commit.
Like real branches, it can be divided into further branches. Pointer of every branch (the name of the branch) will keep moving to the latest commit you make on it. You can work on each branch independently e.g. to work on different versions independently e.g. for Windows XP and Windows 7. Imagine one base Linux with all different flavors as different branches.
You can visualize commits and branches using ‘git show-branch’ command similar to the picture below.


In this picture green, blue, yellow and purple lines are different branches visualized using a software.
More details Creating And Playing With Branches | Git Branch | GitGuys
Also see What a Branch Is for details.


The HEAD in Git is the pointer to the current branch reference, which is in turn a pointer to the last commit you made. It’s generally simplest to think of it as HEAD is the snapshot of your last commit.

Tag (OR Bookmark)



Branches can get complicated, tag is there to save you.** Tag is like a bookmark. **It is created to point to a specific commit anywhere on any branch. Unlike branch e.g. master, which moves forward to point to latest commit, a tag always point to the specified commit.
A tag represents a version of a particular branch at a moment in time.
For more details Git Object: Tag | Git Tag | GitGuys.com
[Source: Git objects: the tag]

Index or Staging Area



The Index is your proposed next commit. It’s basically a loading dock where you get to determine what changes get shipped away. Since the working directory and what gets saved by Git are essentially decoupled, this allows the developer to build up their commits however they want. Since there’s a layer between when Git actually saves the data, you gain a lot more flexibility and control.
More details What is the deal with the Git Index? What is the Git Index? - GitGuys

Checkout



Checking out a commit makes the entire working directory match that commit. This can be used to view an old state of your project without altering your current state in any way. Checking out a file lets you see an old version of that particular file, leaving the rest of your working directory untouched.
[image: http://www.ossxp.com/doc/gotgit-en/

Most of the images and text is taken from [git ready ” learn git one commit at a time] and [Pro Git - Pro Git Book] and blatantly edited and posted by me.
Other sources to learn git the easy way
Aha! Moments When Learning Git
Git documentation and Git tutorials on GitGuys | GitGuys.com

No comments:

Post a Comment