View Commits in a Specific Date Range

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a powerful version control system that allows developers to keep track of changes made to their codebase. One of the most useful features of Git is the ability to view commits in a specific date range. This can be helpful when trying to track down a bug or understand the history of a project.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/SetupandConfigGroup(["Setup and Config"]) git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/SetupandConfigGroup -.-> git/clone("Clone Repo") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/clone -.-> lab-12774{{"View Commits in a Specific Date Range"}} git/log -.-> lab-12774{{"View Commits in a Specific Date Range"}} end

Cloning the Git Repository

To begin exploring Git's date range filtering capabilities, we first need a Git repository to work with. We will use the git-playground repository provided by LabEx.

Let's start by cloning the repository:

  1. Open your terminal in the LabEx VM.
terminal
  1. Run the following command to clone the repository:
git clone https://github.com/labex-labs/git-playground

You should see output similar to this:

Cloning into 'git-playground'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 8 (delta 0), reused 8 (delta 0), pack-reused 0
Receiving objects: 100% (8/8), done.
  1. Navigate to the repository directory:
cd git-playground

Now that we have the repository on our local machine, we can start exploring the commit history.

Exploring Basic Git Log Command

Now that we have our repository cloned, let's learn how to view the commit history using the git log command.

The git log command displays a list of all commits in the repository, starting with the most recent one. Each commit entry includes:

  • A unique commit hash (identifier)
  • Author information
  • Date and time of the commit
  • Commit message

Let's view the basic commit history:

git log

You should see output similar to the following:

commit d22f46ba8c2d4e07d773c5126e9c803933eb5898 (HEAD -> master, origin/master, origin/feature-branch, origin/HEAD)
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file2.txt

commit cf80005e40a3c661eb212fcea5fad06f8283f08f
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file1.txt

commit b00b9374a7c549d1af111aa777fdcc868d8a2a01
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:00 2023 +0800

    Initial commit

If the output is long, you can navigate through it using:

  • Press Space to move forward
  • Press b to move backward
  • Press q to quit the log view

Note that each commit has a unique identifier (the long hexadecimal string), the author's information, the date and time of the commit, and a message describing what changes were made.

Viewing Commits in a Specific Date Range

Now we will learn how to filter commits based on specific dates. Git provides two useful options for this purpose:

  • --since or --after: Shows commits more recent than a specific date
  • --until or --before: Shows commits older than a specific date

When we combine these options, we can view commits within a specific date range.

Let's view all commits that occurred between April 25, 2023, and April 27, 2023:

git log --since='Apr 25 2023' --until='Apr 27 2023'

This command will display all commits that were made between April 25 and April 27, 2023. The output should look like this:

commit d22f46ba8c2d4e07d773c5126e9c803933eb5898 (HEAD -> master, origin/master, origin/feature-branch, origin/HEAD)
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file2.txt

commit cf80005e40a3c661eb212fcea5fad06f8283f08f
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file1.txt

commit b00b9374a7c549d1af111aa777fdcc868d8a2a01
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:00 2023 +0800

    Initial commit

Git accepts many date formats, including:

  • "YYYY-MM-DD" (e.g., 2023-04-25)
  • "Month DD YYYY" (e.g., Apr 25 2023)
  • "DD Month YYYY" (e.g., 25 Apr 2023)

Try another date format to see if there are any commits within a different range:

git log --since='2023-04-20' --until='2023-04-24'

This command might not return any results if there were no commits during that period, which is perfectly normal.

Using Relative Dates and Formatting Options

Git also supports relative dates, which can be very convenient for quickly viewing recent activity.

Let's view all commits from the last 12 weeks:

git log --since='12 weeks ago'

Depending on when you're running this command, you might see all commits or just some of them if they fall within that time frame.

Other useful relative date formats include:

  • "X days ago"
  • "X months ago"
  • "yesterday"
  • "last week"

Let's try viewing commits from the last year:

git log --since='1 year ago'

This command will show all commits made within the past year.

Additional Formatting Options

Git log provides various formatting options to customize the output. Here are a few useful ones:

  1. To display a more concise log with each commit on a single line:
git log --oneline --since='Apr 25 2023' --until='Apr 27 2023'

The output will look like:

d22f46b (HEAD -> master, origin/master, origin/feature-branch, origin/HEAD) Added file2.txt
cf80005 Added file1.txt
b00b937 Initial commit
  1. To see the files that were changed in each commit:
git log --name-status --since='Apr 25 2023' --until='Apr 27 2023'

This command shows the status of files that were modified in each commit, which can be helpful for understanding what was changed.

These formatting options can be combined with date filters to create powerful queries that help you understand the history of a project more effectively.

Summary

In this lab, you learned how to view commits in a specific date range using Git. You explored:

  • How to clone a Git repository to your local machine
  • How to use the basic git log command to view commit history
  • How to filter commits based on specific date ranges using --since and --until options
  • How to use relative dates like "12 weeks ago" for more flexible filtering
  • How to customize the git log output with formatting options

These skills are essential for understanding project history, tracking down bugs, and reviewing the development timeline of a codebase. As you continue working with Git, these filtering techniques will help you navigate even large repositories with ease.