The 12 Days of Git, Day 3: Viewing Git History

FacebooktwitterlinkedinmailFacebooktwitterlinkedinmailby feather

On the third day of Christmas, my true love gave to me… three views of history.

We are slowly building up our Twelve Days of Git song, and today we are going to learn about viewing Git history.

In case you missed the first two verses, here’s the story so far:

  1. The 12 Days of Git: Learn Git over the Holidays
  2. The 12 Days of Git, Day 2: Tracking Files with Git

So far, we have prepared a folder with a web project to use Git for version tracking, and we have started tracking versions of a couple of files, index.html and main.css, in that folder.

Now, let’s see how those files look different when they are being tracked by Git. In a terminal, make the Santa Tracker folder your current working directory and list its contents:

cd ~/Documents/santa-tracker
ls -al

You should use whatever path you used when you created your folder, but be sure to use those flags on the ls command to show the long listing of all the files. You should see output similar to this:

drwxr-xr-x   6 vwilson  staff   204 Dec 20 16:02 .
drwx------+ 15 vwilson  staff   510 Dec 19 10:07 ..
drwxr-xr-x  13 vwilson  staff   442 Dec 20 14:23 .git
-rw-r--r--   1 vwilson  staff   217 Dec 19 10:54 index.html
-rw-r--r--   1 vwilson  staff    88 Dec 19 10:57 main.css

So, what so special about that, aside from the hidden .git folder?

One Way to View History

Git has its own special command for viewing the history of the files it versions. The simplest version is

git log

When you type that in the terminal, you’ll see output like this.

commit 8bfbaa2f4b65ba2b4d27ccdcb79c7eb5e199fb99
Author: Your Name <youremail@yourdomain.com>
Date:   Sun Dec 27 14:23:18 2015 -0500
    Add first files to the repository

Each time you commit a set of changed files in Git, it records the author, the date and time, and the message that you type in. It also creates a unique identifier for every commit, called a commit hash. That’s the long string of characters at the beginning of the log entry. Commit hashes will become useful as you start to use more advanced Git commands, but you can ignore them for now.

Two Ways to View History

If you want to see exactly which lines of which files changed in a changeset, you can add the -p flag to the git log command. (The “p” stands for “patch”, which a standard way for display differences in files.) Try it now.

log -p

The output will be like this:

commit 8bfbaa2f4b65ba2b4d27ccdcb79c7eb5e199fb99
Author: Your Name <youremail@yourdomain.com>
Date: Sun Dec 20 14:23:18 2015 -0500
Add first files to the repository
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..de1667d
--- /dev/null
+++ b/index.html
@@ -0,0 +1,11 @@
 +<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Santa Tracker</title>
+    <link rel="stylesheet" type="text/css" href="main.css">
+</head>
+<body>
+    <h1>Santa Tracker</h1>
+</body>
+</html>
diff --git a/main.css b/main.css
new file mode 100644
index 0000000..f0898fd
--- /dev/null
+++ b/main.css
@@ -0,0 +1,8 @@
+body {
+    background-color: #222;
+    color: #f0f0f0;
+}
+
+h1 {
+    text-align: center;
+}

Besides the normal log information, Git lists the lines that changed in each file because of this commit. Lines that were removed are prefixed with a minus (-) sign. Lines that were added are prefixed with a plus (+) sign. If you just change something in a line, Git will list the old version of the line as being removed, and the new version of the line as being added, because Git works in terms of lines.

Three Ways to View History

The -p can be useful to help track down what changed in each changeset, but it’s an awful lot of information to parse. In fact, the regular output of the log command can become unwieldy as your list of changests grows. Lucky for us, there are several options for the log that show less, but more precise information about the project’s history.

Here is one handy example of several log options that combine to make a very concise yet useful version of history.

git log --graph --decorate --pretty=oneline --abbrev-commit --all

Surprisingly, all those options yield less, not more, output.

* 8bfbaa2 (HEAD, master) Add first files to the repository

For each commit, we see the first seven characters of the commit hash and the commit message. The list will also indicate which commit is the current version (the HEAD), and which commits have a named branch pointing to them. (Git repositories always start with one branch called “master”). We’ll go over branches on a later day.

That’s an awful lot to remember and type every time, just to get concise output; so, let’s end this day with a shortcut. You only have to type the following once:

git config --global alias.lol 'log --graph --decorate --pretty=oneline --abbrev-commit --all'

After doing that, you can simply type

git lol

and you will see the same concise output as if you had typed all those options out.

To see all the options available for the command we’ve learned today, check them out in the online Git reference:

Check in tomorrow for The 12 Days of Git, Day 4: Double-checking Changes with Git

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.