What happens when you git commit ?

Absolute Beginner | Simple Introduction to GIT ( Part I)

Version Control

Imagine you are a video editor and you just completed a job on a big youtuber’s video. But  feel like trying out some really cool editing technique, so  his/her subscribers would notice your artistry and want to find out more about you and possibly hire you for more work. Here are possible scenarios independent of version control system:

  • The new special effect was added, saved ,tested and was really cool. You send it to the YouTuber and he/she likes it very much and uploads it. everyone is happy.
  • Same as above but he/she says she prefers the previous version (without the special effect). Problem is: you did not save a copy of the pre-special effect video, so you only have the post-version. Darn it.
  • You made a copy, added the new special effect to the copy … everyone is happy.
  • Did the exact same thing but you suddenly got another idea. So you saved a third copy (1 original, first-special-effect copy, 2nd-special-effect-copy) .. this repeats like 10x: Problem : your storage

Personal Note: For a very simple project such as a presentation, I personally do not use GIT. I just often work with the copy and paste. However, VCS really shows its strength when collaboration and increased project complexity is in the picture. E.g. a Building a Web App  [Frontend Team, Backend Team, Web Designers, UX/UI Team, Q/A Testing Team] are all involved, ideally.

Summary of GIT VCS’ job

  • records changes made to a file
  • Permits reverting back to a specific version/state
  • makes comparing states
  • favours collaboration

Basic Terminology

Repositoryall tracking information are stored in form of git objects in the repository. This can be initiated by running either the git init or git clone <remote repository url> command
BranchBranching allows you to make a copy of a certain state of a directory. This is useful when you want to add a really cool feature unto a stable project but is untested. Working on a copy instead of the original project would be a safer route.
Remote repositoryNot local. Usually described as repositories that are hosted somewhere on the internet e.g. repositories on github, gitlab etc.
Local repositorySimply a repository hosted on your local machine.
staging (area)This is a step before the commit step. When you run git add <file>, you are adding the file changes into the staging area. Sort of like; “hey git, I just want you to know about these changes I have made to this file. I plan on making a snapshot (commit) later”.
ConfigAs the name suggest, it stores basic configuration information of the repository. Here you could set the repository owner name ad email (contact informaton), so just incase your repository goes public, people could figure out who created it.
DescriptionBasically the frontend of your repository. What ever you write in the description file will be displayed in the gitweb (frontend: what users see).
ObjectThis is where all the core tracked files are found. The most important types of objects created by git are blobs (the file contents squashed and compressed), tree object ( subdirectories information compressed : required to map out the directory structure) and the commit object ( contains pointer to the tree object, author info and commiter info).
HooksA really nice feature in git is the ability to call actions before or/and after know git commands (e.g. pull, rebase, commit etc). These actions are called hooks. If you go inside the directory ( cd hooks) and list the files, try opening one (notepad <filename>).
HeadContains reference to the current branch. Since this repository was just created, the HEAD (current) branch is most likely the master branch.

VCS with Git Scenarios

Scenario I : Just you

initialize git

  • You just began writing your thesis and you are want to use git to manage the changes before the day of submission.
  • A master_thesis.docx or master_thesis.txt is created in directory /home/you/masterthesis/
  • After installing git, as instructed here, you initialise it by running the git init command in your command line.
  • A .git folder is now in your /home/you/masterthesis directory, which contains all necessary information for tracking the changes to your files within /home/you/masterthesis.
  • As shown above, there are 4 directories ( hooks, info, objects and refs) and the config, description and HEAD files.
  • This is all you really need to know as a beginner. There are more though, so do personal research if needed.

Track file/directory changes

  • You want to know what files within you directory are untracked or tracked. Simply run git status in the command line. Most likely, your master_thesis.docx file is untracked. Meaning git is not following the changes made to this file. To track it, you first run git add master_thesis.docx and then snapshot it by running git commit -m <message here>.
  • Every time you commit a file, git creates a hash via a specific algorithm (if a previous commit exists, compares the new hash against the previous one to see if changes were made) and stores the hash within the /objects directory.

view commit objects

  • In order to view the commits you have made within a repository, simply run  git log. The objects created are the commit, tree and blob object. So in this case, since this is the first commit, 3 objects are stored in /objects.

Add more changes, stage and snapshot again

  • Now you added a title, logo, table of contents and chapter names to your document. So you decide to stage this state of your thesis again: git add  and git commit -m .
  • Apart from the creation of a second commit, tree and 1 blob object, something else occurs. The new commit object, which already points to its own tree object, also points to the commit object immediately before it.

New branches

  • Let’s say you want to try out another word template and change the font, in the absence of vcs you would simply make a copy of the thesis and continue working on it. In git terms it is called “Branching”.
  • After creating a 2nd branch, you need to move your HEAD pointer to the new branch. Because if you do not explicitly tell HEAD to move to <newbranch>, any commit you make after will still be “connected” to the default branch master, a behaviour you probably do not want.
  • By using the git checkout <newbranchname> command, the HEAD pointer points to your new branch, meaning you are safe to make your template and font changes.


First commit

What happens when you git commit ?
What happens when you git commit ?

Second commit

What happens on subsequent commits ?

Creating a new branch

Creating new Branch
What happens when you create a new git branch and commit

Useful Commands

git repository

  • Initialise git repository in a given directory : git init
  • Copying a remote git repository into you local machine while adding a reference to the remote repository : git clone
  • Checking the tracked and untracked files within your repository : git status

git staging area

  • to track files or add to the staging / cache area :  git add <filename>
  • if however, you made changes, staged them but do not want them anymore = empty out the staging area : git reset 

git branches

  • Shows a list of all branches of a repository; highlights the one pointed by HEAD: git branch
  • Delete a repository: git branch -d or –delete
  • Moves HEAD pointer to another branch/ switch active branch: git checkout <branchname>
  • Create a new branch and immediately switch into it: git checkout -b <new branchname>

git help

  • Get information on a specific git command : git <command> –help

making snapshots

  • Takes a snapshot of all staged changes, creates commit, tree and blob objects: git commit -m
  • Stages or temporarily “caches” given file changes: git add <file>

git remote repositories

  • Shows a list of remote repositories linked to local repository: git remote
  • Links a remote repository to the local repository: git remote add <remote alias> <remote repository url>
  • Removes connection to a linked remote repository: git remote remove <remote alias>

In part II, We will learn about other git commands such as pull, fetch, merge and I will be describing a VCS collaboration scenario


Interesting Blog Posts

Operating System :  Windows 10

Exercise:  How would you stop all tracking without deleting the directory ? TIP (you have to delete something)