How to use the command 'git log' (with examples)

How to use the command 'git log' (with examples)

Git log is a command that allows you to view the commit history of a Git repository. It provides a detailed overview of the commits made, including the commit message, author, and timestamp. This command is useful for tracking changes, understanding the development timeline, and identifying specific commits or changes within a repository.

Use case 1: Show a history of commits

Code:

git log

Motivation: The ‘git log’ command is used to display a chronological list of commits made in a Git repository. This can be helpful for understanding the changes made over time, tracking the development progress, and reviewing the commit history before making further changes.

Explanation: No additional arguments are required for this use case. By simply running ‘git log’ in the command line, Git will display the commit history starting from the latest commit. The commits are listed in reverse chronological order, with the most recent commit displayed first.

Example output:

commit abcdef1234567890
Author: John Doe <johndoe@example.com>
Date:   Tue Apr 20 09:00:00 2022 +0300

    Added feature XYZ

commit 0123456789abcdef
Author: Jane Smith <janesmith@example.com>
Date:   Mon Apr 19 14:30:00 2022 +0300

    Fixed bug ABC

...

Use case 2: Show the history of a particular file or directory, including differences

Code:

git log -p path/to/file_or_directory

Motivation: Sometimes, it is necessary to review the history of a specific file or directory to understand changes and differences over time. This can be helpful when troubleshooting issues, identifying when specific changes were made, or understanding the evolution of a particular file or directory within the repository.

Explanation: By specifying the path to a file or directory after the ‘-p’ flag, Git will display the commit history related to that file or directory, including the differences made in each commit. The ‘-p’ flag stands for “patch” which shows the differences introduced in each commit.

Example output:

commit abcdef1234567890
Author: John Doe <johndoe@example.com>
Date:   Tue Apr 20 09:00:00 2022 +0300

    Modified file1.txt

diff --git a/path/to/file/file1.txt b/path/to/file/file1.txt
index 0123456..abcdef1 100644
--- a/path/to/file/file1.txt
+++ b/path/to/file/file1.txt
@@ -1,4 +1,4 @@
 This is some content
-In file1.txt
+Modified in commit abcdef

commit 0123456789abcdef
Author: Jane Smith <janesmith@example.com>
Date:   Mon Apr 19 14:30:00 2022 +0300

    Added file2.txt

...

Use case 3: Show an overview of which file(s) changed in each commit

Code:

git log --stat

Motivation: When working on a project, it is useful to have an overview of which files were modified in each commit. This can help identify the scope of changes, understand the impact of a specific commit, or review the activity on specific files within the repository.

Explanation: The ‘–stat’ flag provides a summary of file modifications in each commit. It displays the file name and the number of lines added or deleted in that commit. This information gives a quick overview of the changes made to various files within the repository.

Example output:

commit abcdef1234567890
Author: John Doe <johndoe@example.com>
Date:   Tue Apr 20 09:00:00 2022 +0300

    Modified file1.txt

 file1.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit 0123456789abcdef
Author: Jane Smith <janesmith@example.com>
Date:   Mon Apr 19 14:30:00 2022 +0300

    Added file2.txt

 file2.txt | 1 +
 1 file changed, 1 insertion(+)

...

Use case 4: Show a graph of commits in the current branch using only the first line of each commit message

Code:

git log --oneline --graph

Motivation: Visualizing the commit history in a graph format is beneficial for understanding the branching and merging patterns within a repository. Showing only the first line of the commit message provides a concise overview of the purpose or nature of each commit.

Explanation: The ‘–oneline’ flag condenses each commit into a single line, displaying only the first line of the commit message. The ‘–graph’ flag generates a textual graph representation that visually represents the commit history, including branches and merges.

Example output:

* abcdef1 (HEAD -> main) Added feature XYZ
* 0123456 Fixed bug ABC
|\
| * hijklmn Added file2.txt
* | 7890abc Modified file1.txt
...

Use case 5: Show a graph of all commits, tags, and branches in the entire repository

Code:

git log --oneline --decorate --all --graph

Motivation: When working with a complex repository that includes multiple branches and tags, having an overview of the entire commit history can be helpful for understanding the relationship between branches, tracking the evolution of tags, and visualizing the overall structure of the repository.

Explanation: The ‘–decorate’ flag adds additional information to the commit history, displaying tags and branch names. The ‘–all’ flag includes all branches, tags, and other references in the repository. Combining these options with ‘–oneline’ and ‘–graph’ results in a visually appealing commit history representation.

Example output:

* abcdef1 (HEAD -> main, tag: v1.0.0) Added feature XYZ
* 0123456 (tag: v0.9.0) Fixed bug ABC
|\
| * hijklmn (branch2, tag: v0.8.0) Added file2.txt
* | 7890abc (branch1) Modified file1.txt
...

Use case 6: Show only commits whose messages include a given string (case-insensitively)

Code:

git log -i --grep search_string

Motivation: Searching for specific commits based on the commit message can be useful when trying to locate changes related to a particular feature, fixing a bug, or investigating an issue. This use case allows case-insensitive searching, making it easier to find relevant commits.

Explanation: The ‘-i’ flag in combination with ‘–grep’ allows searching for commits case-insensitively. By specifying the ‘search_string’, Git will filter the commit history and display only the commits whose messages contain the specified string.

Example output:

commit abcdef1234567890
Author: John Doe <johndoe@example.com>
Date:   Tue Apr 20 09:00:00 2022 +0300

    Added feature XYZ and fixed XYZ-123 bug

commit 0123456789abcdef
Author: Jane Smith <janesmith@example.com>
Date:   Mon Apr 19 14:30:00 2022 +0300

    Fixed bug XYZ

...

Use case 7: Show the last N commits from a certain author

Code:

git log -n number --author=author

Motivation: When working on a collaborative project, it is sometimes necessary to track the contributions made by a specific author. This can be useful for reviewing their work, addressing issues related to their changes, or analyzing the development activity of a particular contributor.

Explanation: By specifying the ’number’ of commits using the ‘-n’ flag and the ‘author’ using the ‘–author’ flag, Git will display the last N commits made by the specified author. This command filters the commit history based on the author’s name or email address.

Example output:

commit abcdef1234567890
Author: John Doe <johndoe@example.com>
Date:   Tue Apr 20 09:00:00 2022 +0300

    Added feature XYZ

commit 0123456789abcdef
Author: John Doe <johndoe@example.com>
Date:   Mon Apr 19 14:30:00 2022 +0300

    Fixed bug ABC

...

Use case 8: Show commits between two dates (yyyy-mm-dd)

Code:

git log --before="2022-01-29" --after="2022-01-17"

Motivation: To analyze a specific time range within the commit history, it can be helpful to filter commits between two given dates. This use case allows tracking changes made during a particular period, reviewing the activity within a certain timeframe, or identifying commits related to a specific release.

Explanation: The ‘–before’ flag specifies the upper date boundary, while the ‘–after’ flag defines the lower date boundary. This command filters the commit history and displays only the commits made within the specified date range.

Example output:

commit abcdef1234567890
Author: John Doe <johndoe@example.com>
Date:   Tue Jan 25 09:00:00 2022 +0300

    Added feature XYZ

commit 0123456789abcdef
Author: Jane Smith <janesmith@example.com>
Date:   Mon Jan 24 14:30:00 2022 +0300

    Fixed bug ABC

...

Conclusion:

The ‘git log’ command is a powerful tool for viewing and analyzing the commit history of a Git repository. From displaying a chronological list of commits to searching for specific changes or filtering commits based on various criteria, this command provides valuable insights into the development process and the evolution of a project. Understanding and utilizing the different use cases of ‘git log’ can greatly enhance the productivity and effectiveness of working with Git repositories.

Related Posts

How to use the command 'pkgctl release' (with examples)

How to use the command 'pkgctl release' (with examples)

The pkgctl release command is used to perform a release step that involves committing, tagging, and uploading build artifacts.

Read More
How to use the command Get-Help (with examples)

How to use the command Get-Help (with examples)

Get-Help is a command in PowerShell that allows you to display help information and documentation for PowerShell commands, including aliases, cmdlets, and functions.

Read More
How to use the command wine (with examples)

How to use the command wine (with examples)

The wine command allows users to run Windows executables on Unix-based systems.

Read More