Home Development Tools git git extracts time interval modification files

git extracts time interval modification files

May 20, 2023 am 09:48 AM

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
Copy after login

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'
Copy after login

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
Copy after login

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
Copy after login

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(+)
Copy after login

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
Copy after login

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.
Copy after login

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Git vs. GitHub: Version Control and Code Hosting Git vs. GitHub: Version Control and Code Hosting Apr 11, 2025 am 11:33 AM

Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions and supports local operations; GitHub provides online collaboration tools such as Issue tracking and PullRequest.

Is Git the same as GitHub? Is Git the same as GitHub? Apr 08, 2025 am 12:13 AM

Git and GitHub are not the same thing. Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions, and GitHub provides an online collaboration environment.

How to download git projects to local How to download git projects to local Apr 17, 2025 pm 04:36 PM

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

Is GitHub difficult to learn? Is GitHub difficult to learn? Apr 02, 2025 pm 02:45 PM

GitHub is not difficult to learn. 1) Master the basic knowledge: GitHub is a Git-based version control system that helps track code changes and collaborative development. 2) Understand core functions: Version control records each submission, supporting local work and remote synchronization. 3) Learn how to use: from creating a repository to push commits, to using branches and pull requests. 4) Solve common problems: such as merge conflicts and forgetting to add files. 5) Optimization practice: Use meaningful submission messages, clean up branches, and manage tasks using the project board. Through practice and community communication, GitHub’s learning curve is not steep.

How to update code in git How to update code in git Apr 17, 2025 pm 04:45 PM

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

How to use git commit How to use git commit Apr 17, 2025 pm 03:57 PM

Git Commit is a command that records file changes to a Git repository to save a snapshot of the current state of the project. How to use it is as follows: Add changes to the temporary storage area Write a concise and informative submission message to save and exit the submission message to complete the submission optionally: Add a signature for the submission Use git log to view the submission content

What to do if the git download is not active What to do if the git download is not active Apr 17, 2025 pm 04:54 PM

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

Should I put Git or GitHub on my resume? Should I put Git or GitHub on my resume? Apr 04, 2025 am 12:04 AM

On your resume, you should choose to write Git or GitHub based on your position requirements and personal experience. 1. If the position requires Git skills, highlight Git. 2. If the position values ​​community participation, show GitHub. 3. Make sure to describe the usage experience and project cases in detail and end with a complete sentence.

See all articles