History

Goals

  • Learn how to view the history of the project.

Getting a listing of what changes have been made is the function of the git log command.

Execute:

  1. git log

You should see …

Output:

  1. $ git log
  2. commit 4254c9466673a6ddbeb35e2802315dbcfc1dbb62
  3. Author: Jim Weirich <jim (at) edgecase.com>
  4. Date: Sat Jun 20 20:37:05 2020 +0100
  5. Added a comment
  6. commit c8b3af1cdc72ea8162d08f182a3e8f69a1ebf9b2
  7. Author: Jim Weirich <jim (at) edgecase.com>
  8. Date: Sat Jun 20 20:37:05 2020 +0100
  9. Added a default value
  10. commit 30c2cd4b78087f4c6fc8c464e490af6894454c08
  11. Author: Jim Weirich <jim (at) edgecase.com>
  12. Date: Sat Jun 20 20:37:05 2020 +0100
  13. Using ARGV
  14. commit 4445720e0ab1008f680c49a26f6392ce15a4dae0
  15. Author: Jim Weirich <jim (at) edgecase.com>
  16. Date: Sat Jun 20 20:37:05 2020 +0100
  17. First Commit

Here is a list of all four commits that we have made to the repository so far.

One Line Histories

You have a great deal of control over exactly what the log command displays. I like the one line format:

Execute:

  1. git log --pretty=oneline

You should see …

Output:

  1. $ git log --pretty=oneline
  2. 4254c9466673a6ddbeb35e2802315dbcfc1dbb62 Added a comment
  3. c8b3af1cdc72ea8162d08f182a3e8f69a1ebf9b2 Added a default value
  4. 30c2cd4b78087f4c6fc8c464e490af6894454c08 Using ARGV
  5. 4445720e0ab1008f680c49a26f6392ce15a4dae0 First Commit

Controlling Which Entries are Displayed

There are a lot of options for selecting which entries are displayed in the log. Play around with the following options:

  1. git log --pretty=oneline --max-count=2
  2. git log --pretty=oneline --since='5 minutes ago'
  3. git log --pretty=oneline --until='5 minutes ago'
  4. git log --pretty=oneline --author=<your name>
  5. git log --pretty=oneline --all

See man git-log for all the details.

Getting Fancy

Here’s what I use to review the changes made in the last week. I’ll add --author=jim if I only want to see changes I made.

  1. git log --all --pretty=format:'%h %cd %s (%an)' --since='7 days ago'

The Ultimate Log Format

Over time, I’ve decided that I like the following log format for most of my work.

Execute:

  1. git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

It looks like this:

Output:

  1. $ git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
  2. * 4254c94 2020-06-20 | Added a comment (HEAD -> master) [Jim Weirich]
  3. * c8b3af1 2020-06-20 | Added a default value [Jim Weirich]
  4. * 30c2cd4 2020-06-20 | Using ARGV [Jim Weirich]
  5. * 4445720 2020-06-20 | First Commit [Jim Weirich]

Let’s look at it in detail:

  • --pretty="..." defines the format of the output.
  • %h is the abbreviated hash of the commit
  • %d are any decorations on that commit (e.g. branch heads or tags)
  • %ad is the author date
  • %s is the comment
  • %an is the author name
  • --graph informs git to display the commit tree in an ASCII graph layout
  • --date=short keeps the date format nice and short

This is a lot to type every time you want to see the log. Fortunately we will learn about git aliases in the next lab.

Other Tools

Both gitx (for Macs) and gitk (any platform) are useful in exploring log history.