Viewing commit history in Git
Start your free 7-days trial now!
To view commit history in Git we can use the git log
command which prints out all commits which have been done until now, showing the most recent entries first.
Examples
Repository commit history
To view the commit history for the repository you are currently in:
git log
commit a4708a6a7d593e6119247baf4b126ca68dcf6417Author: Harry Potter <example@email.com>Date: Thur Oct 7 22:55:13 2021 -0700 Update version numbercommit ab5adad5f531b039f3771ed7eae8e6de1b073995Author: Harry Potter <example@email.com>Date: Mon Oct 4 17:45:31 2021 -0700 Making initial changescommit f24tqh25b3e450402ge8563abf99ad00de2209e6Author: Harry Potter <example@email.com>Date: Sat Oct 2 11:12:18 2021 -0700 Initial commit
Notice the log shows:
unique ID for each commit (called a hash)
the author of each commit
the date of the commit
the commit message
Specific file / directory commit history
To only view the commit history for a particular file or directory:
git log <path>
where <path>
represents the filepath for the particular files or directories you would like to view the commit history for.
For example, to just view the commit history for a file called example.txt
:
git log /Users/Harry/Desktop/project/example.txt
commit ab5adad5f531b039f3771ed7eae8e6de1b073995 (HEAD -> master)Author: Harry Potter <example@email.com>Date: Thu Oct 7 12:57:08 2021 +0900 second version
Display only latest x commits
To display only the latest x commits, we can pass -
followed by the number of commits to restrict the output to. For example, to display only the latest 3 commits for example.txt
we can run:
git log -3 /Users/Harry/Desktop/project/example.txt