Let us continue our discussion from previous post and look into some of the commands. We will focus on key concepts and this is in no way a replacement for git documentation.

To quickly recap, git is a persistent map at its core and is meant to track content. Git calculates and saves SHA-1 checksums of different kinds of content and stores it its object database. It uses 4 objects to track/store content; blob, tree, commit and tag. A branch is simply a reference to a commit while HEAD is a reference to currently checked-out branch (or commit). Current branch tracks the new commit (branch reference is updated to point to latest commit made). There are 3 main areas; working directory, index and repository.

Let us first understand what “index” is. Index or staging is a holding place of sorts between the working directory and repository. Any changes/updates that we make in working directory, need to be “staged” before they can be committed/tracked in repository. Conceptually, we can think of index as a table of blob/tree objects with their hash values in repository, index and working tree. See this article for a better understanding of index.

To understand any git command we need to know a) How it moves information between different areas and b) What it does to repository.

git checkout

Most of the time we start by checking out a branch.

What is moved?

  • Based on commit that is referred to by the branch, git retrieves snapshot of working directory from the repository and copy it to index and working directory
  • Index and working directory will be in sync after checkout

What is tracked?

  • HEAD is updated to point to the branch checked out

git status

This will show what is added/updated/removed from the working tree as compared to commit pointed to by the HEAD reference.

What is moved?

  • Refreshes the working tree column of index based on changes made in working directory.

What is tracked?

  • No changes to repository

git add

Marks changes specified for next commit. We can only commit what has been added/staged.

What is moved?

  • Refreshes the working tree/index column of index based on one or more paths specified

What is tracked?

  • No changes to repository

git commit

As the name indicates, this will commit changes to repository. This now becomes part of history.

What is moved?

  • Creates git objects (blob, tree) based on changes added to index
  • Creates a commit record with commit message and other information

What is tracked?

  • Currently checked out branch reference is updated to point to just created commit hash

Happy coding.