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:
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.
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.
In Git, we can use time interval to represent a period of time. There are several ways to express time intervals, such as:
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:
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:
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:
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!