search
HomeDevelopment Toolsgitgit extracts time interval modification files

Git is a very popular version control tool that allows us to easily manage code modifications and collaboration. And when we need to extract the modification history of a specified file within a period of time, how should we do it? Today, we will introduce how to use Git commands to extract file modification records within a time interval.

First of all, we need to understand some basic knowledge of Git:

  1. Git submission record

In Git, every code submission will be recorded and Save it as a commit record, that is, a commit. Each commit has a unique hash value that is used to distinguish different commit records.

  1. Git submission history

All submission records are saved in the Git warehouse, forming a submission history. We can use Git commands to view the submission history, including the hash value, author, submission time, modified files and other information of each submission.

  1. Git time interval

In Git, we can use time interval to represent a period of time. There are several ways to express time intervals, such as:

  • Date: 2019-01-01
  • Relative time: 2 weeks ago
  • Time period: 2019-01 -01..2019-12-31

After understanding these basic knowledge, let’s explore how to extract file modification records within the time interval.

Suppose we have the following submission history:

commit 2942849f3f44e6da111d2b58fe6c24c156835c90
Author: John Doe <john@example.com>
Date:   Mon Feb 22 11:27:23 2021 +0800

    Add README.md

commit e0fdb2b0364a73f347f1a1b46c13bfbd5ac07a08
Author: John Doe <john@example.com>
Date:   Sat Feb 20 14:52:32 2021 +0800

    Fix typo in index.html

commit 71f6be88b1562fe596c69b88ac9f72dbeac78786
Author: Alice Zhang <alice@example.com>
Date:   Fri Feb 19 22:33:12 2021 +0800

    Update index.html

commit 275711456d94c307b6416d94da74e5c8b98497c7
Author: Bob Chen <bob@example.com>
Date:   Thu Feb 18 09:45:43 2021 +0800

    Add new feature

commit fd9bf6e768bb58d2a1ebf0b604805d7cca34c563
Author: Alice Zhang <alice@example.com>
Date:   Wed Feb 17 15:18:02 2021 +0800

    Initial commit

We want to extract all modified README.md files whose submission time was between February 18th and February 21st. You can use the following command:

git log --since=2021-02-18 
        --until=2021-02-21 
        --pretty=format:"%h - %an, %ar : %s" 
        --name-only 
        --grep='README.md'

Command analysis:

  • git log: View submission history
  • --since=2021-02-18: Limit viewing only Submissions starting from 2021-02-18
  • --until=2021-02-21: Limit viewing to only submissions up to 2021-02-21
  • --pretty=format:" %h - %an, %ar : %s": Specify the printing format, including the submitted hash value, author, submission time and submission information
  • --name-only: only display the modified file name , do not display the modified content of the file
  • --grep='README.md': only search for submission records containing README.md

After executing the above command, the following output will be Result:

2942849 - John Doe, 29 minutes ago : Add README.md
e0fdb2b - John Doe, 2 days ago : Fix typo in index.html

As you can see, only two submission records meet the time interval and grep conditions. If we want to see the specific files modified in these submissions, we can add a --stat option:

git log --since=2021-02-18 
        --until=2021-02-21 
        --pretty=format:"%h - %an, %ar : %s" 
        --name-only 
        --grep='README.md' 
        --stat

Command analysis:

  • --stat: Display the list of files modified by each submission and the overall situation of file modifications

After executing the above command, the following results will be output:

2942849 - John Doe, 29 minutes ago : Add README.md
 README.md | 1 +
 1 file changed, 1 insertion(+)

e0fdb2b - John Doe, 2 days ago : Fix typo in index.html
 README.md | 2 ++
 1 file changed, 2 insertions(+)

You can see that the first submission The README.md file was added, and the second submission modified the README.md file and added two lines of content.

In addition to the above methods of viewing submission history, we can also use the git blame command to view the modification history of files, including the hash value and author of the submission.

git blame README.md 
        --since=2021-02-18 
        --until=2021-02-21

Command analysis:

  • git blame: View the modification history of the file
  • README.md: Specify the file to be viewed
  • --since =2021-02-18: Limit to view only the modification history from 2021-02-18
  • --until=2021-02-21: Limit to view only the modification history until 2021-02-21

After executing the above command, the following results will be output:

2942849f (John Doe 2021-02-22 11:27:23 +0800 1) This is a README file.
e0fdb2b0 (John Doe 2021-02-20 14:52:32 +0800 2) It contains information about the project.
e0fdb2b0 (John Doe 2021-02-20 14:52:32 +0800 3)
e0fdb2b0 (John Doe 2021-02-20 14:52:32 +0800 4) Update: fix typo.

As you can see, the first and fourth lines are two different submissions, both submitted by John Doe submitted.

To summarize, we can use Git commands to extract file modification records within a time interval. These commands are easy to master and only require an understanding of some basic concepts and parameters. When we need to view the code modification history within a certain period of time, these commands can help us quickly locate the relevant submission records and view the modified files and content.

The above is the detailed content of git extracts time interval modification files. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to use git management tools for complete usage of git management toolsHow to use git management tools for complete usage of git management toolsMar 06, 2025 pm 01:32 PM

This article provides a guide to Git management, covering GUI tools (Sourcetree, GitKraken, etc.), essential commands (git init, git clone, git add, git commit, etc.), branch management best practices (feature branches, pull requests), and merge con

How to push the specified commitHow to push the specified commitMar 06, 2025 pm 01:39 PM

This guide explains how to push a single Git commit to a remote branch. It details using a temporary branch to isolate the commit, pushing this branch to the remote, and then optionally deleting the temporary branch. This method avoids conflicts and

How to view commit contentsHow to view commit contentsMar 06, 2025 pm 01:41 PM

This article details methods for viewing Git commit content. It focuses on using git show to display commit messages, author info, and changes (diffs), git log -p for multiple commits' diffs, and cautions against directly checking out commits. Alt

The difference between commit and push of gitThe difference between commit and push of gitMar 06, 2025 pm 01:37 PM

This article explains the difference between Git's commit and push commands. git commit saves changes locally, while git push uploads these committed changes to a remote repository. The article highlights the importance of understanding this distin

How to solve the failure of git commit submissionHow to solve the failure of git commit submissionMar 06, 2025 pm 01:38 PM

This article addresses common Git commit failures. It details troubleshooting steps for issues like untracked files, unstaged changes, merge conflicts, and pre-commit hooks. Solutions and preventative measures are provided to ensure smoother Git wo

The difference between add and commit of gitThe difference between add and commit of gitMar 06, 2025 pm 01:35 PM

This article explains the distinct roles of git add and git commit in Git. git add stages changes, preparing them for inclusion in the next commit, while git commit saves the staged changes to the repository's history. This two-step process enables

How to use git management tools Tutorial for using git management tools for beginnersHow to use git management tools Tutorial for using git management tools for beginnersMar 06, 2025 pm 01:33 PM

This beginner's guide introduces Git, a version control system. It covers basic commands (init, add, commit, status, log, branch, checkout, merge, push, pull) and resolving merge conflicts. Best practices for efficient Git use, including clear comm

What is git code management tool? What is git code management tool?What is git code management tool? What is git code management tool?Mar 06, 2025 pm 01:31 PM

This article introduces Git, a distributed version control system. It highlights Git's advantages over centralized systems, such as offline capabilities and efficient branching/merging for enhanced collaboration. The article also details learning r

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment