Git Viewing Commit History
The two commonly used commands for viewing Git commit history are:
- git log - View the historical commit records.
- git blame <file> - View the historical modification records of a specified file in list format.
git log
After making several updates with Git commits, or after cloning a project, if you want to review the commit history, you can use the git log
command.
Based on the operations from the previous chapter, the historical commit records are listed as follows using the git log
command:
$ git log
commit d5e9fc2c811e0ca2b2d28506ef7dc14171a207d9 (HEAD -> master)
Merge: c68142b 7774248
Author: tutorialpro <[email protected]>
Date: Fri May 3 15:55:58 2019 +0800
Merge branch 'change_site'
commit c68142b562c260c3071754623b08e2657b4c6d5b
Author: tutorialpro <[email protected]>
Date: Fri May 3 15:52:12 2019 +0800
Modified code
commit 777424832e714cf65d3be79b50a4717aea51ab69 (change_site)
Author: tutorialpro <[email protected]>
Date: Fri May 3 15:49:26 2019 +0800
Changed the tutorialpro.php
commit c1501a244676ff55e7cccac1ecac0e18cbf6cb00
Author: tutorialpro <[email protected]>
Date: Fri May 3 15:35:32 2019 +0800
We can use the --oneline
option to view a concise version of the historical records.
$ git log --oneline
d5e9fc2 (HEAD -> master) Merge branch 'change_site'
c68142b Modified code
7774248 (change_site) Changed the tutorialpro.php
c1501a2 Removed test.txt, added tutorialpro.php
3e92c19 Added test.txt
3b58100 First version commit
This tells us the development history of the project.
We can also use the --graph
option to see when branches and merges occurred. The following is the same command with the topological graph option enabled:
* d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\
| * 7774248 (change_site) Changed the tutorialpro.php
* | c68142b Modified code
|/
* c1501a2 Removed test.txt, added tutorialpro.php
* 3e92c19 Added test.txt
* 3b58100 First version commit
Now we can more clearly see when the work diverged and merged.
You can also use the --reverse
parameter to display all logs in reverse order.
$ git log --reverse --oneline
3b58100 First version commit
3e92c19 Added test.txt
c1501a2 Removed test.txt, added tutorialpro.php
7774248 (change_site) Changed the tutorialpro.php
c68142b Modified code
d5e9fc2 (HEAD -> master) Merge branch 'change_site'
If you only want to find the commit logs of a specific user, you can use the command: git log --author
, for example, let's say we want to find the parts of the Git source code that Linus committed:
$ git log --author=Linus --oneline -5
81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory
3bb7256 make "index-pack" a built-in
377d027 make "git pack-redundant" a built-in
b532581 make "git unpack-file" a built-in
112dd51 make "mktag" a built-in
If you want to specify a date, you can use several options: --since
and --before
, but you can also use --until
and --after
.
For example, if I want to see all commits in the Git project from three weeks ago and after April 18th, I can execute this (I also used the --no-merges
option to hide merge commits):
$ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
5469e2d Git 1.7.1-rc2
d43427d Documentation/remote-helpers: Fix typos and improve language
272a36b Fixup: Second argument may be any arbitrary string
b6c8d2d Documentation/remote-helpers: Add invocation section
5ce4f4e Documentation/urls: Rewrite to accommodate transport::address
00b84e9 Documentation/remote-helpers: Rewrite description
03aa87e Documentation: Describe other situations where -z affects git diff
77bc694 rebase-interactive: silence warning when no commits rewritten
636db2c t3301: add tests to use --format="%N"
For more git log
commands, see: http://git-scm.com/docs/git-log
git blame
git blame <file>
The git blame
command displays modification records in list format, as shown in the following example:
$ git blame README
^d2097aa (tianqixin 2020-08-25 14:59:25 +0800 1) # tutorialpro Git Test
db9315b0 (tutorialpro 2020-08-25 16:00:23 +0800 2) # tutorialpro.org