Notes of Maks Nemisj

Experiments with JavaScript

GIT: Learn by doing. Intro

Hi there,

This is an introduction to my series of articles regarding Git. It contains essential information to understand the other chapters. Ofcourse if you know all this you can skip to the next chapter.

There is no “spoon”

Probably you’ve been working with Git already for a long time, but for now, I want to ask you to forget about all the stuff you do with Git (like creating branches, committing to the branch, merging branches etc). Forget about “origin” and the fact that Git has branches.

Task 1:

Do you remember how your git history looks like? Probably something like this (see below), where you have, branch names and that “origin” word:
git history with branches
Git history you normally see
Now, imagine the same history, but only with these different strange numbers every commit has and without any human branches, like this:
git history with hashes
Git history you should try to see

There is only a commit

You know that Git has commits, and this is what it is all about in this chapter. It’s all about a commit. There is only a commit. There is so much more in a commit itself that I want to tell you and maybe you don’t know.

Let’s have a look at what is a commit. You could compare a commit to a point in time associated with a group of actions that happened before. You can imagine that a commit is like your daily activity – wake up, wash your face, get dressed, etc.

The number of these “dots” in time is completely up to you and you decide when one dot contains one action or multiple.
human history in dots
Human history is a chain of “dots” ( events )
This is also what we do to our source code. Instead of washing a face, we wash out some code from a file, rename a file, change code…but you know that already.
source code history in dots
Source code history is a chain of “dots” ( actions )
We create commits every day, but maybe not in the way I described above – without thoughtfully thinking, “How much of these actions should I put into one commit and how much of them will I put into another one”.

As soon as you move on with the chapters, you will notice that thinking this way is essential to better understand and work with Git.

This is a time machine

One of the most significant powers which Version Control Systems (VCS) gives us is the ability to return in time.
Git is not different in that, and for that purpose, I will call Git a “Time machine.”

You can go back in time and replay the whole history of your actions, from start to the end.

By going back in time to an individual commit, you analyze the state of the code at that particular time, you can feel the code, and you can smell it. This is precisely what command checkout is doing in Git.
Checkout dot 2
Return back to dot 2 -“Rename file a to b”
To return to a “dot” number 2 ( Rename file a to b ) we would execute:

git checkout 2

One important thing to note. Do you remember I told you to forget about branches in Git? Good, with the command above, we don’t checkout a branch with name 2, instead we checkout a commit itself. The number 2 in this case is an id of that commit. They also call it “a commit hash.”

What is even more critical to understand is that Git also allows us to create a new “dot” from that point in time, meaning we can create a new alternate history, like in the movie “Back to the Future Part II“.
Doc Brown explains future alternation
Now, imagine we will change the file z again and commit it.

git commit -m "Add file z"

Alternate git history with a new commit
Alternate git history with a new commit
(I will keep repeating. We have no branches yet. There is only a new commit.)

As you’ve done that (currently only in your brain 🙂 ), you might think that all other stuff, which you had in git history before, is gone … actually, it’s not. All the previous commits are still available, but not that easily visible in Git.
Previous history is still available
Previous history is still available
Doesn’t it remind you of a picture with Doc Brown from above? 😉 I told you, it’s a time machine.

This all means that commit 3a together with a previous commits 2 and 1 will start forming a fork and the whole git history will look like a tree.

It’s so bad we can’t do that in real life, look how cool could it be 😀 :
Time machine
My Daily time machine
So if you want to see what would happen if you do first ‘get dressed’ and then ‘wash your face’, do it and don’t be afraid, git remembers it all ( even without branches )

This is precisely the power that Git gives us. We can experiment without worrying about the consequences. We can keep changing history, committing, going back, and committing again.

I repeat – don’t be afraid to “travel in time” with Git. If you start looking at these dotted lines in your Git clients and see the commits instead of branches you’ve used to, you’ll see the power of a time machine git provides to you.

What is in the hash

Since history might become huge with all the different paths, Git doesn’t use numeric values to give commits a number. Instead, Git uses hashes to identify the commits.
commit has hashes
Git hashes
What is very important to remember is that hash is a UNIQUE element of a commit. No matter what you do, you can always rely on it being a unique value.

That’s why every change to Git history, will CHANGE the hash of that commit. Even if you redo the same action again, Git will still see it as a new “dot” in time, meaning a new commit, and a new hash. Such a situation is also shown in the picture above – “File z” has been added again as a new commit “3a”

With all this information now you can easily go into any commit in your git history, by running checkout with commit hash at the end. You will need to remember this. This is very essential for future articles.

git checkout 69e13aa8

Let’s recap what have we learned so far.
  • It’s possible to travel in time, by specifying commit hash to checkout command
  • There is no need in “branches” to create a “branch” like history
  • Commit has a unique hash
  • Two commits with the same content will deliver 2 unique and different hashes
In the next chapter, you will learn how to create branches without a name and how to see what is not visible in the Git history. Additionally, it will be a more practical chapter than this one.

Chapter 1 is here


Leave a Reply

Your email address will not be published. Required fields are marked *