Git Log Git

Jan 15th, 2021 - written by Kimserey with .

Git log is a command used to look at the commit logs. It’s a useful command to look at the history of a repository, the different commits, the different merges or even the repository graph. In today’s post we will look at few example where git log can be used.

Log History

In order to show the commit history we simply do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
❯ git log

commit 09d3b89d2f0c74af3e7d5ef51576853a82565e2a (HEAD -> rebase-a, master)
Author: Kimserey Lam
Date:   Sat Jan 16 10:09:52 2021 +0000

    M

commit 0302e5620cc8daf9f8c539b24ff01348d6844133
Merge: a231419 909cacb
Author: Kimserey Lam
Date:   Sat Jan 16 10:08:31 2021 +0000

    L - Merge branch 'feature-b'

commit a23141958f6da0c3ed303acd9dab5e8b8fc3693a
Author: Kimserey Lam
Date:   Sat Jan 16 10:06:07 2021 +0000

    K

which shows the whole history in a pager. If we want just a few number of commits back we can use -{n}:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
❯ git log -2

commit 09d3b89d2f0c74af3e7d5ef51576853a82565e2a (HEAD -> rebase-a, master)
Author: Kimserey Lam
Date:   Sat Jan 16 10:09:52 2021 +0000

    M

commit 0302e5620cc8daf9f8c539b24ff01348d6844133
Merge: a231419 909cacb
Author: Kimserey Lam
Date:   Sat Jan 16 10:08:31 2021 +0000

    L - Merge branch 'feature-b'

which show five commits. The commit log will show the full content of the log message, if we want to see a reduced version, we can use --oneline:

1
2
3
4
5
6
7
❯ git log -5 --oneline

09d3b89 (HEAD -> rebase-a, master) M
0302e56 L - Merge branch 'feature-b'
a231419 K
cb38acd Merge branch 'feature-a'
1670262 I

which will show a oneline including the hash and title of the commit. Because this is a condensed view, we can use --no-pager to temporarily disable the pager:

1
❯ git --no-pager log -5 --oneline

Another interesting option is -p which shows the changes brought by the commit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 git log -3 -p

commit 09d3b89d2f0c74af3e7d5ef51576853a82565e2a (HEAD -> rebase-a, master)
Author: Kimserey Lam
Date:   Sat Jan 16 10:09:52 2021 +0000

    M

diff --git a/a.md b/a.md
index 6038a34..cbe9b3f 100644
--- a/a.md
+++ b/a.md
@@ -15,3 +15,4 @@ hello
 hello
 hello
 hello
+hello

commit 0302e5620cc8daf9f8c539b24ff01348d6844133
Merge: a231419 909cacb
Author: Kimserey Lam
Date:   Sat Jan 16 10:08:31 2021 +0000

    L - Merge branch 'feature-b'

commit a23141958f6da0c3ed303acd9dab5e8b8fc3693a
Author: Kimserey Lam
Date:   Sat Jan 16 10:06:07 2021 +0000

    K

So far we’ve been dispalying the log of the current checkout branch, if we want to see all logs in the repository, we can use --all:

1
❯ git log --all

Or if we want to see selective branches, we can specify them:

1
❯ git log -5 --oneline master feature-1

Lastly --merges can be used to only display merge commits:

1
❯ git log -5 --oneline --merges

Log Diff

Git log can also be used to see extra commits present in a branch compared to another branch or commits mutually exclusive to both branches.

git log double dot

For example:

1
❯ git log --oneline master..feature-1

will show the extra logs present in feature-1 which aren’t currently in master.

In contrast:

1
❯ git log --oneline master...feature-1

... (triple dot) will show the logs present in master that aren’t in feature-1, and the log in feature-1 that aren’t in master.

git log triple dot

Graph

Lastly another interesting command from git log is to show the repository graph with --graph:

1
❯ git log -5 --graph --oneline

this will display a text-based graphical representation of the commit history.

And that concludes today’s post! See you on the next one!

External Sources

Designed, built and maintained by Kimserey Lam.