Skip to content
Home » git log: Understanding and Mastering Your Repository’s History

git log: Understanding and Mastering Your Repository’s History

Git log is a powerful tool that allows you to view the history of your repository, including all the commits made, their authors, and the associated commit messages. It can be an invaluable resource for understanding the changes made to your codebase and tracking down any issues that may arise.

Here are some key points to remember about git log:

  • It displays the commit history of your repository in reverse chronological order (most recent commits first).
  • It allows you to filter the output by commit author, date range, or specific commit message.
  • It can be used to display only a summary of the commit (e.g. the commit hash and message) or a more detailed view including the diff of the changes made in the commit.

Did you know that you can also use git log to view the history of a specific file or directory within your repository? Simply pass the --follow flag along with the file or directory path to see the commit history for that specific path.

Here are 5 ways you can use git log:

  1. View the commit history of a specific file or directory to understand the changes made to that path over time.
  2. Use the -author flag to filter the output by commit author and quickly see the contributions made by a specific team member.
  3. Use the -since and -until flags to view the commit history within a specific date range, useful for understanding changes made during a specific sprint or project.
  4. Use the -oneline flag to view a concise summary of the commit history, making it easier to quickly scan through large amounts of commits.
  5. Use the -grep flag to search for specific keywords in the commit messages, useful for tracking down commits related to a specific issue or feature.

Now it’s time to test your knowledge! Can you provide an example command that displays the commit history for a specific file, filtered by commits made within the last week and sorted by the author’s email address?

Examples

git log

Use case: View the commit history of your repository in reverse chronological order

Output:

commit 8b3f94b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: John Doe <john.doe@example.com>
Date:   Mon Jan 1 10:00:00 2018 -0500

    Added new feature

commit d3f3b3b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: Jane Doe <jane.doe@example.com>
Date:   Sun Dec 31 15:00:00 2017 -0500

    Fixed bug

git log --oneline

Use case: View a concise summary of the commit history

Output:

8b3f94b Added new feature
d3f3b3b Fixed bug

git log --author "John Doe"

Use case: Filter the commit history by commit author

Output:

commit 8b3f94b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: John Doe <john.doe@example.com>
Date:   Mon Jan 1 10:00:00 2018 -0500

    Added new feature

git log --since "2 weeks ago" --until "1 week ago"

Use case: View the commit history within a specific date range

Output:

commit 8b3f94b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: John Doe <john.doe@example.com>
Date:   Mon Jan 1 10:00:00 2018 -0500

    Added new feature

git log --follow path/to/file

Use case: View the commit history of a specific file or directory

Output:

commit 8b3f94b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: John Doe <john.doe@example.com>
Date:   Mon Jan 1 10:00:00 2018 -0500

    Added new feature to path/to/file

commit d3f3b3b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: Jane Doe <jane.doe@example.com>
Date:   Sun Dec 31 15:00:00 2017 -0500

    Fixed bug in path/to/file

git log --grep "fix"

Use case: Search for specific keywords in the commit messages

Output:

commit d3f3b3b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: Jane Doe <jane.doe@example.com>
Date:   Sun Dec 31 15:00:00 2017 -0500

    Fixed bug

git log -p

Use case: View the diff of the changes made in each commit

Output:

commit 8b3f94b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: John Doe <john.doe@example.com>
Date:   Mon Jan 1 10:00:00 2018 -0500

    Added new feature

diff --git a/path/to/file b/path/to/file
index e69de29..8b3f94b 100644
--- a/path/to/file
+++ b/path/to/file
@@ -0,0 +1 @@
+New feature added

commit d3f3b3b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: Jane Doe <jane.doe@example.com>
Date:   Sun Dec 31 15:00:00 2017 -0500

    Fixed bug

diff --git a/path/to/file b/path/to/file
index d3f3b3b..e69de29 100644
--- a/path/to/file
+++ b/path/to/file
@@ -1 +0 @@
-Buggy code

git log --graph

Use case: View the commit history in a graphical format, showing branches and merges

Output:

* commit 8b3f94b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
  | Author: John Doe <john.doe@example.com>
  | Date:   Mon Jan 1 10:00:00 2018 -0500
  |
  |     Added new feature
  |
* commit d3f3b3b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
  | Author: Jane Doe <jane.doe@example.com>
  | Date:   Sun Dec 31 15:00:00 2017 -0500
  |
  |     Fixed bug

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

Use case: Customize the output format of git log

Output:

8b3f94b - John Doe, 2 days ago : Added new feature
d3f3b3b - Jane Doe, 3 days ago : Fixed bug

git log --reverse

Use case: View the commit history in chronological order (oldest commits first)

Output:

commit d3f3b3b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: Jane Doe <jane.doe@example.com>
Date:   Sun Dec 31 15:00:00 2017 -0500

    Fixed bug

commit 8b3f94b3d87b33c5dbbe1df7fd1b4a87a0da0a4f
Author: John Doe <john.doe@example.com>
Date:   Mon Jan 1 10:00:00 2018 -

Additional Resources

Git log is just the tip of the iceberg when it comes to understanding and using Git effectively. If you’re looking to learn more, here are some additional resources to check out:

Challenge

Can you provide an example command that displays the commit history for a specific file, filtered by commits made within the last week and sorted by the author’s email address?

Hint: You can use the --since flag to filter by date range, the --author flag to sort by author, and the --pretty=format flag to customize the output format.

Leave a Reply

Your email address will not be published. Required fields are marked *

18 − sixteen =