Getting information on commits

Author

Marie-Hélène Burle

Before we can make use of old commits, we need to be able to get information about them. This is critical to know how to navigate the history of a project.

Displaying the commit history

Do you remember this diagram from the first section of this course?

This is very useful to get a map of the history of our project. But how can we get a visual for it? The command here is git log and its many options.

In its basic form, it outputs the logs of our past commits:

git log
commit ca3c0360bd8ab961117671dd1f8fb2d2c3d5d7a1 (HEAD -> main)
Author: Marie-Helene Burle <xxx@xxx>
Date:   Tue Jul 11 23:08:59 2023 -0700

    Add first draft of script

commit c4ab5e755179a7b28a09c0ca587551bdac504d35
Author: Marie-Helene Burle <xxx@xxx>
Date:   Tue Jul 11 23:06:40 2023 -0700

    Add .gitignore file with data and results

commit 61abf96298b54baf6d48cdea2ab1477db1075b5e
Author: Marie-Helene Burle <xxx@xxx>
Date:   Mon Jul 10 23:23:25 2023 -0700

    Initial commit

As you can see, commits are listed from the bottom up.

Commits can be displayed as one-liners:

git log --oneline
ca3c036 (HEAD -> main) Add first draft of script
c4ab5e7 Add .gitignore file with data and results
61abf96 Initial commit

Or as a graph:

git log --graph
* commit ca3c0360bd8ab961117671dd1f8fb2d2c3d5d7a1 (HEAD -> main)
| Author: Marie-Helene Burle <xxx@xxx>
| Date:   Tue Jul 11 23:08:59 2023 -0700
|
|     Add first draft of script
|
* commit c4ab5e755179a7b28a09c0ca587551bdac504d35
| Author: Marie-Helene Burle <xxx@xxx>
| Date:   Tue Jul 11 23:06:40 2023 -0700
|
|     Add .gitignore file with data and results
|
* commit 61abf96298b54baf6d48cdea2ab1477db1075b5e
  Author: Marie-Helene Burle <xxx@xxx>
  Date:   Mon Jul 10 23:23:25 2023 -0700

      Initial commit

Or in any fancy way you like:

git log \
    --graph \
    --date=short \
    --pretty=format:'%C(cyan)%h %C(blue)%ar %C(auto)%d'`
                   `'%C(yellow)%s%+b %C(magenta)%ae'
* ca3c036 31 minutes ago  (HEAD -> main)Add first draft of script xxx@xxx
* c4ab5e7 34 minutes ago Add .gitignore file with data and results xxx@xxx
* 61abf96 24 hours ago Initial commit xxx@xxx

Run man git-log for a full list of options.

Information about a commit

git log is useful to get an overview of our project history, but the information we get about each commit is limited. To get additional information about a particular commit, you can use git show followed by the hash of the commit you are interested in.

For instance, let’s explore our last commit

git show
commit ca3c0360bd8ab961117671dd1f8fb2d2c3d5d7a1 (HEAD -> main)
Author: Marie-Helene Burle <xxx@xxx>
Date:   Tue Jul 11 23:08:59 2023 -0700

    Add first draft of script

diff --git a/src/script.py b/src/script.py
new file mode 100644
index 0000000..263ef67
--- /dev/null
+++ b/src/script.py
@@ -0,0 +1,7 @@
+import pandas as pd
+from matplotlib import pyplot as plt
+
+df = pd.read_csv('../data/dataset.csv')
+
+df.plot()
+plt.savefig('../results/plot.png', dpi=300)

Or our second commit:

git show c4ab5e7  # Replace the hash by the hash of your second commit
commit c4ab5e755179a7b28a09c0ca587551bdac504d35
Author: Marie-Helene Burle <xxx@xxx>
Date:   Tue Jul 11 23:06:40 2023 -0700

    Add .gitignore file with data and results

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e85f44a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/data/
+/results/

In addition to displaying the commit metadata, git show also displays the diff of that commit with its parent commit.