Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Advanced Git Log Techniques

Recommended resources

Hello! Welcome to your first lesson in our course on mastering Git and GitHub for team collaboration. Your experience with the basics of Git provides an excellent foundation. Now, we'll start building on that to get you comfortable with the workflows and tools used in collaborative projects.

This first module focuses on "Advanced Git Inspection." Before you can confidently manage branches, resolve conflicts, or contribute to a team's codebase, you need to be able to accurately read and understand the project's history. This lesson will equip you with that skill by diving deep into one of Git's most fundamental commands: git log. We will move beyond its default output and learn how to use its powerful formatting and filtering options to find exactly the information you need.

Let's begin.

Your Most Powerful Historical Tool: git log

You're likely familiar with running git log to see a list of recent commits. By default, it gives you a detailed, multi-line view for each commit, showing the commit hash, author, date, and full commit message. This is useful, but it can be overwhelming.

The true power of git log lies in its ability to be customized. We can broadly categorize these customizations into two actions:

  1. Formatting: Changing how the commit history is displayed.
  2. Filtering: Changing which commits are shown in the output.

The video below gives a great introduction to the default git log output, the "pager" (the scrollable view Git uses for long output), and this distinction between formatting and filtering.

Full Git Tutorial (Part 7) - Git Log Fully Explained

Watch this segment from the "Full Git Tutorial" by Ben Kadel. It will reinforce your existing knowledge and set the stage for the more advanced options we're about to explore.

Please watch from the beginning until the introduction of formatting and filtering. Pay attention to the explanation of the default output and how to navigate and exit the pager by pressing q.


Formatting the Log for Clarity

The default git log output is verbose. When working on a project with a complex history involving multiple developers and branches, you need a more concise and visual representation. Let's explore some flags to achieve that.

The Essentials: --oneline, --graph, and --decorate

These three flags are your bread and butter for getting a quick, high-level overview of the repository's structure.

  • --oneline: Condenses each commit into a single line, showing just the abbreviated commit hash and the commit message title.
  • --graph: Draws an ASCII graph in the left margin, showing the branching and merging history.
  • --decorate: Shows the names of branches and tags next to their corresponding commits.

While useful individually, their real power comes from combining them. On its own, git log shows you the history of your currently checked-out branch. To see the history of all branches, you can add the --all flag.

Try running this command in one of your own project repositories:

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

You'll see something that resembles this image, giving you a clear, bird's-eye view of all the work happening in the repository.

This is the output of `git log` combined with `--graph`, `--oneline`, and `--decorate`. It visually represents the commit history, including branches, merges, and where HEAD and remote branches are pointing.

For a deeper look at these and other formatting options, the following reading from Atlassian's Git tutorials is an excellent resource.

Advanced Git Log | Atlassian Git Tutorial

This guide provides a clear, structured explanation of the most useful formatting options for git log.

Please read the section on formatting options. Focus on understanding --oneline, --decorate, --stat, --graph, and the basics of custom formatting with --pretty=format. These are the tools you'll use most frequently.


Filtering the Log to Find What You Need

Formatting helps you see the history more clearly, but filtering helps you find the proverbial needle in the haystack. When you need to answer questions like "Who changed this file last?" or "What work did my teammate do yesterday?", filtering is the tool you'll reach for.

Let's look at the most common filtering scenarios you'll encounter in a team environment.

  • By Author: To see commits from a specific person.
    git log --author="John"
  • By Date: To see work within a specific timeframe. You can use absolute dates ("2023-10-26") or relative ones ("1 week ago").
    git log --since="yesterday"
  • By Commit Message: To find commits related to a specific task, assuming your team uses a convention like including a ticket number (e.g., "JIRA-123").
    git log --grep="JIRA-123"
  • By File: To see the history of changes for a specific file. This is incredibly useful for debugging.
    git log -- path/to/your/file.js
  • By Range: To see the commits on one branch that are not yet on another. This is essential for seeing what changes a pull request will introduce.
    git log main..feature-branch

The Atlassian guide we looked at earlier also provides an excellent overview of these filtering techniques.

Advanced Git Log | Atlassian Git Tutorial

This part of the guide covers the various ways you can slice and dice your commit history to find specific commits.

Now, read the section on filtering options. As you read, try running these commands on your own repository. Pay special attention to filtering by author, file, and the concept of a commit range, as these are particularly vital for collaborative work.

For a quick video demonstration of these filtering techniques in action, you can also watch this short clip.

Advanced Git Tutorial - Interactive Rebase, Cherry-Picking, Reflog, Submodules and more

This segment from a freeCodeCamp tutorial provides a rapid-fire demonstration of the most common filtering flags.

Watch the section from how to filter your commit history. This will visually reinforce the concepts you just read about.


Conclusion

In this lesson, we transformed git log from a simple history viewer into a powerful inspection tool. You now have the ability to not only make the commit history more readable but also to surgically search it for the exact information you need.

Here are the key takeaways:

  • You can customize git log output in two main ways: formatting (how it looks) and filtering (what it shows).
  • The command git log --graph --oneline --decorate --all provides a powerful, high-level overview of the entire repository's history and branch structure.
  • You can filter your history by a variety of criteria, including author, date, file path, and commit message content.
  • Using commit ranges like main..feature-branch allows you to see the differences between branches, a crucial skill for team collaboration.

As you move forward, practice using these options. Make them a part of your daily Git workflow. The more comfortable you are with inspecting history, the more confident you will be when making changes.

In our next lesson, we will continue to build your inspection skills by looking at git diff. While git log shows you that a change happened, git diff will show you exactly what the change was, line by line.