History

Logging

As we already know, Git keeps track of every commit that we make. Because of that, we can easily review them in the so-called log.

$ git log

//Output Below

commit ff698ab
Author: Elliot <elliot@allsafe.com>
Date:   Tue Nov 15 14:28 2016

    Add cookie

Nice! git log brings up the commit history, where every commit is displayed with a unique hash code.

Logging again

Let's commit again to see multiple commits in the Git log. This time, let's also add a message.

$ touch cat.gif
$ git add cat.gif
$ git commit -m "Hello friend"
$ git log

//Output Below

commit 7c1b0c4
Author: Elliot <elliot@allsafe.com>
Date:   Tue Nov 15 14:49 2016

    Hello friend

commit ff698ab
Author: Elliot <elliot@allsafe.com>
Date: Tue Nov 15 14:28 2016

    Add cookie

Nice! Now git log reveals both of the commits we've made so far. From the commit messages we get an idea of what we've changed.

HEAD

The so-called HEAD is a pointer that points to the current commit.

$ git show HEAD

//Output Below

commit 7c1b0c4
Author: Elliot <elliot@allsafe.com>
Date: Tue Nov 15 14:49 2016

    Add cat

Sweet! Using git show, we can see that the HEAD is pointing to the commit with the hash code 7c1b0c4.

Turning back time

We can easily go back to a previous commit and put the working directory back to the state it was in at the time.

$ git checkout f871379 

//Output Below

Note: checking out 'f871379'.
You are in 'detached HEAD' state.
HEAD is now at f871379... Add cookie

Perfect! Now (almost) everything is like it was when we made that commit.

Psst: we can either use the first seven characters or the whole hash code.

Lost in time

When we go back to a previous commit, we can see each file as it was when we made the commit but adding or changing files in there isn't a good idea.

$ git checkout f871379
Note: checking out 'f871379'.
You are in 'detached HEAD' state.
HEAD is now at f871379... Add cookie.txt
$ touch cake.txt
$ git status 

//Output Below

HEAD detached at f871379
Untracked files:
 
    cake.txt

See that? Now the current commit isn't the latest commit anymore, which means we're in detached HEAD state, in which committing can lead to data losses.

Back to the future

Be gone, detached HEAD, we're going back to the future!

$ git checkout master 

//Output Below

Previous HEAD position was f871379
Add cookie.txt
Switched to branch 'master'

Great Scott, that's it! Using checkout master, we can go back to the latest commit, where everything is okay again.

Psst: now the HEAD points to the latest commit again.

Tagging

Instead of using and having to remember hash codes, we can also put tags on commits.

$ git checkout f871379

  HEAD is now at f871379
  Add cookie and cake

$ git tag v1.2
$ git tag

//Output Below

v1.2

See that? We use git tag with a parameter to add a tag and the command without any parameters to display the previously added tags.

Checking out tags

We can also use tags in combination with the checkout command.

$ git tag
v1.2
$ git checkout v1.2 

//Output Below

Note: checking out 'v1.2'.
You are in 'detached HEAD' state.
HEAD is now at f871379
Add cookie and cake

Again, we are back at commit f871379. Using tags can help us memorize important commits.