Git - Viewing Commit History


Git has the ability to keep a detailed history of changes to your project over time. By recording each change in a commit, Git allows developers to track, revert, and understand how their codebase has evolved.

The commit history is a key resource for both individual developers and teams working on a project, enabling them to review past changes, debug issues, and collaborate effectively.

The Importance of Commit History

Commit history in Git is essentially a timeline of snapshots, each representing a point in the project's development. By viewing the commit history, you can:

  • Understand the evolution of your project: See how your codebase has changed over time and who made those changes.

  • Track down bugs: Find out when and where a bug was introduced by reviewing specific commits.

  • Revert to previous states: If a recent change causes problems, you can revert to an earlier, stable version of the code.

  • Collaborate efficiently: Teams can see what others are working on, review code changes, and ensure that the project remains on track.

The `git log` Command

You can check commit history in Git by using git log command, which provides a complete overview of changes done, making it easier to follow development progress.

The git log shows a list of commits, starting from the latest, along with information such as commit hash, author, date and commit message.

To view commit history in Git, run the following command:

$ git log
$ git log
commit 94744071e847ab78390eb1b8a5e013dbeee7ced2 (HEAD -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 13:01:47 2024 +0530

   changed the width dimension

commit a98962782a8bc6ac91b793b634eb5786b3fb85fc
Author: tonystark <[email protected]>
Date:   Sat Dec 10 08:35:10 2022 +0000

   Initial commit
:...skipping...

This simple log view gives you a linear history of commits, with the most recent changes appearing at the top.

The git log> command displays the default commit history view, which includes:

  • Commit hash: A unique identifier (SHA-1 hash) for the commit.

  • Author: The name and email address of the person who made the commit.

  • Date: The date and time when the commit was made.

  • Commit message: A short description provided by the author explaining what the commit includes.

The Git log command shows several ways to view your repository's history.

Browsing Commit History by File

Sometimes, you may want to view the history of a particular file rather than the entire project. Git allows you to do this with the git log command and the file name:

git log <file>

If you want to see the diffs for each commit affecting the file, you can add the -p flag:

$ git log -p -2

In above command, -p option displays a detailed view of the commit and -2 shows the last two commits.

$ git log -p -2
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (HEAD -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 17:51:17 2024 +0530

   changed top and right dimensions

diff --git a/main.html b/main.html
index f7f85dd..9b97bad 100644
--- a/main.html
+++ b/main.html
@@ -46,8 +46,8 @@
            display : inline-block;
            border  : 2px solid green;
            position: absolute;
-           right:45px;
            top: 36px;
+           right:42px; ^M
+           top:35px; ^M
:...skipping...
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 18:56:17 2024 +0530

   changed top and right dimensions

diff --git a/main.html b/main.html
index f7f85dd..9b97bad 100644
--- a/main.html
+++ b/main.html
@@ -46,8 +46,8 @@

You can use the git log --stat command to display a brief overview of the changes made in each commit, including the number of files altered, the number of lines added or removed, and the file name.

$ git log --stat
$ git log --stat
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (HEAD -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 17:51:17 2024 +0530

   changed top and right dimensions

main.html  |  4 ++--
1 file changed, 2 insertions(+), 2 deletion(-)

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

main.html  |  4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author: Tutorialspoint <[email protected]>
:...skipping...
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 13:01:47 2024 +0530

   changed top and right dimensions

If you want a complete overview of all the changes made in each commit, including which specific lines were added, removed, or modified.

$ git log --stat -p
$ git log --stat -p
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (HEAD -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 17:51:17 2024 +0530

   changed top and right dimensions
---
main.html  |  4 ++--
1 file changed, 2 insertions(+), 2 deletion(-)

diff --git a/main.html b/main.html
index f7f85dd..9b97bad 100644
--- a/main.html
+++ b/main.html
@@ -46,8 +46,8 @@
            display : inline-block;
            border  : 2px solid green;
            position: absolute;
:...skipping...
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 18:56:17 2024 +0530

Customizing `git log` Output

While the default output of git log is useful, you can customize it to display more or less information depending on your needs. Git provides several options to format and filter the commit history.

One-Line Log

If you want a more concise view of the commit history, showing each commit in a single line, you can use the --oneline flag. This is particularly useful for getting a quick overview of the history.

$ git log --oneline

Example output:

abc1234 Add user authentication feature
def7890 Update README with installation instructions

This view only displays the abbreviated commit hash and the commit message, making it easy to scan through the history quickly.

Filtering by Date or Author

You can filter the commit history by date, author, or a combination of both. This is useful when you are trying to locate a specific commit or review changes made during a particular time period.

Filter by date: To show commits made since a specific date, use the --since option:

git log --since="2024-08-01"

Filter by author: To view commits made by a specific author, use the --author option:

git log --author="Tutorialspoint "

You can combine these options to narrow down the history further. For example, to view all commits made by Tutorialspoint since August 1st, 2024:

git log --author="Tutorialspoint" --since="2024-08-01"

Formatting Commit Output

Git allows you to customize the way each commit is displayed using the --pretty option. You can specify a format or use one of Git's predefined formats.

Short format:

git log --pretty=short

Custom format: You can specify which elements of the commit you want to see:

git log --pretty=format:"%h - %an, %ar : %s"

The command git log --pretty=format:"%h - %an, %ar : %s" is used to display particular information about each commit in a simple manner. It will show each commit on a single line with an abbreviated commit hash, author, date and commit message.

>
  • %h: commit hash

  • %an: author name

  • %ar: date

  • %s: commit message

$ git log --pretty=format:"%h - %an, %ar : %s"
$ git log --pretty=format:"%h - %an, %ar : %s"
09708ca - Tutorialspoint, 15 minutes ago : color and heading changed
45ef43e - Tutorialspoint, 18 hours ago : changed top and right dimensions
9474407 - Tutorialspoint, 19 hours ago : modified the margin and font size
1bc5202 - Tutorialspoint, 23 hours ago : changed the width dimension
a989627 - tonystark, 1 year, 5 months ago : Initial commit

The following table displays helpful specifiers that can be used with the git log --pretty=format command.

Specifier Description of Output
%H Commit hash
%h Abbreviated commit hash
%T Tree hash
%t Abbreviated tree hash
%P Parent hashes
%p Abbreviated parent hashes
%an Author name
%ae Author email
%ad Author date
%ar Author date relative
%cn Committer name
%ce Committer email
%cd Committer date
%cr Committer date relative
%s Subject
  • When you combine the --oneline or --format options with --graph, Git displays anASCII graph next to each commit statement.

  • The command git log --pretty=format:"%h %s" --graph represents each commit abbreviated hash and commit message with a visual representation of the branch and merge history.

$ git log --pretty=format:"%h %s" --graph
$ git log --pretty=format:"%h %s" --graph
* 09708ca color and heading changed
* 45ef43e changed top and right dimensions
* 9474407 modified the margin and font size
* 1bc5202 changed the width dimension
* a989627 Initial commit
  • The command git log --pretty=oneline used to get a quick or short overview of a large number of commits without going through several lines of data for each one.

  • It will show each commit on a single line with an abbreviated commit hash and commit message.

$ git log --pretty=oneline
$ git log --pretty=oneline
45ef43e5c3664f9ff725ad7b41a80af6ab515 (HEAD -> main, origin/main, origin/HEAD) changed top and right dimensions
94744071e847ab78390eb1b8a5e013dbeee7ced2 modified the margin and font size
1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5 changed the width dimension
a98962782a8bc6ac91b793b634eb5786b3fb85fc Initial commit

The following table displays common formatting options for git log:

Options Description of Output
-p Show the patch introduced with each commit.
--stat Show statistics for files modified in each commit.
--shortstat Display only the changed/insertions/deletions line from the --stat command.
--name-only Show the list of files modified after the commit information.
--name-status Show the list of files affected with added/modified/deleted information as well.
--abbrev-commit Show only the first few characters of the SHA-1 checksum instead of all 40.
--relative-date Display the date in a relative format (for example, 3 weeks ago) instead of using the full date format.
--graph Display an ASCII graph of the branch and merge history beside the log output.
--pretty Show commits in an alternate format. Option values include oneline, short, full, fuller, and format (where you specify your own format).
--oneline Shorthand for --pretty=oneline --abbrev-commit used together.

Limiting Log Output

Git log provides some useful limiting options that allow you to display only a subset of commits. You can use -<n> to show the last n commits.

Git automatically directs all output through a pager, allowing you to view only one page of log output at a time.

You can view Git Commit History of specific intervals using time-limiting options such as --since and --until.

The following command displays the commit history since 3 weeks ago:

$ git log --since=3.weeks
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Thu Apr 18 12:15:17 2024 +0530

   color and heading changed

commit 45ef43e5c3664f9ff725ad7b41a80af6ab515
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 18:56:17 2024 +0530

   changed top and right dimensions

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 13:01:47 2024 +0530

The following command displays the commit history since 2022-03-17:

$ git log --since=2022-03-
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <[email protected]>
Date:   Thu Apr 18 12:15:17 2024 +0530

   color and heading changed

commit 45ef43e5c3664f9ff725ad7b41a80af6ab515
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 18:56:17 2024 +0530

   changed top and right dimensions

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author: Tutorialspoint <[email protected]>
Date:   Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

:...skipping...
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (Head -> main, origin/main, origin/HEAD)

You can check the commit lists using search criteria. The --author option identifies a specific author, whereas --grep searches for keywords in commit messages.

Note: You can use --author and --grep options multiple times to filter commits based on different criteria. Adding the --all-match option restricts the output to commits that satisfy all of the given --grep patterns.

The -S option is referred to as Git's "pickaxe". It displays commits that change the count of a given string occurrence.

$ git log -S <string>
$ git log -S font-size
commit a98962782a8bc6ac91b793b634eb5786b3fb85fc
Author :tonystark <[email protected]>
Date : Sat Dec 10 08:35:10 2022 +0000

   Initial commit
  
   Created from https://vercel.com/new

You can specify a directory or file name after the git log --, which will filter the log output to only show commits that changed those files.

$ git log -- path/to/file
$ git log -- main.html
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (HEAD -> main, origin/main, origin/HEAD)
Author :Tutorialspoint <[email protected]>
Date : Thu Apr 18 12:15:17 2024 +0530

   color and heading changed

commit 45ef43e5c3664f9ff725ad7b41a80af6ab515
Author : Tutorialspoint <[email protected]>
Date :   Wed Aug 07 18:56:17 2024 +0530

   changed top and right dimensions

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author :Tutorialspoint <[email protected]>
Date : Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author :Tutorialspoint <[email protected]>
Date : Wed Aug 07 13:01:47 2024 +0530

The following table displays options to limit the output of git log:

Options Description of Output
-<n> Show the patch introduced with each commit.
--since, --after Limit the commits to those made after the specified date.
--until, --before Limit the commits to those made before the specified date.
--author Only show commits in which the author entry matches the specified string.
--committer Only show commits in which the committer entry matches the specified string.
--grep Only show commits with a commit message containing the string.
-S Only show commits adding or removing code matching the string.

To find commits altering test files in Git's source code, committed by Tutorialspoint in the month of April 2024, run the following command:

$ git log --pretty="%h - %s" --author='Tutorialspoint' --since="2024-4-1" --before="2024-4-30"
09708ca - color and heading changed
45ef43e - changed top and right dimensions
9474407 - modified the margin and font size
1bc5202 - changed the width dimension

To find the total size of the commit history, run the following command:

$ git log --log-size
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (HEAD -> main, origin/main, origin/HEAD)
log size: 122
Author :Tutorialspoint <[email protected]>
Date : Thu Apr 18 12:15:17 2024 +0530

   color and heading changed

commit 45ef43e5c3664f9ff725ad7b41a80af6ab515
log size: 130
Author :Tutorialspoint <[email protected]>
Date : Wed Aug 07 18:46:17 2024 +0530

   changed top and right dimensions

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
log size: 130
Author :Tutorialspoint <[email protected]>
Date : Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

:...skipping...

To find the commit history in reverse order, run the following command:

$ git log --reverse
commit a98962782a8bc6ac91b793b634eb5786b3fb85fc
Author: tonystark <[email protected]>
Date: Sat Dec 10 08:35:10 2022 +0000

   Initial commit

   Created from https://vercel.com/

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author: Tutorialspoint <[email protected]>
Date: Wed Aug 07 13:01:47 2024 +0530

   changed the width dimension

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author: Tutorialspoint 
Date: Wed Aug 07 17:51:17 2024 +0530

:...skipping...
commit a98962782a8bc6ac91b793b634eb5786b3fb85fc
Author :tonystark <[email protected]>
Date : Sat Dec 10 08:35:10 2022 +0000

   Initial commit

   Created from https://vercel.com/new
Advertisements