Home  >  Article  >  php教程  >  Git's Patch function

Git's Patch function

黄舟
黄舟Original
2016-12-13 14:16:441307browse

Ordinary developers clone the code from the software warehouse, then write the code, make a patch, and finally send it to Linux by email The maintainer of the Kernel would be nice. Git was originally used as a Linux version control tool, providing transparent, complete, and stable Patch functions.

Let’s first introduce what Patch is. If a software has a new version, we can completely download the new version of the code for compilation and installation. However, like Linux For a large project like Kernel, even if the code is compressed, it exceeds 70MB. Each new download is quite expensive. However, each update may not change more than 1MB of code, so we only To be able to have diff data of two versions of the code, it should be possible to update the program at a very low cost. Therefore, Larry Wall developed a tool: patch. It can perform version updates based on a diff file.

But in git, we don’t need to use diff and patch directly to make patches. Doing so is dangerous and troublesome. Git provides two simple patch solutions. One is to use git The standard patch generated by diff, and the second one is the Git-specific patch generated by git format-patch.

1. Standard patch generated by git diff

We can first make a patch with git diff. There is initially a file a in the working directory of the example in this article, and the content is "This is the file a.", placed in the master branch. In order to modify the code, our general approach is to create a new branch:

sweetdum@sweetdum-ASUS:~/GitEx$ git branch Fix
sweetdum@sweetdum-ASUS:~/GitEx$ git checkout Fix
Switched to branch 'Fix'

Next we append a line to file a and then execute git diff.
sweetdum@sweetdum-ASUS:~/GitEx$ echo 'Fix!!!'>>a
sweetdum@sweetdum-ASUS:~/GitEx$ git diff
diff --git a/a b/a
index 4add65f..0d295ac 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
This is the file a.
+Fix!!!

We saw Git The output of diff, this is a very typical patch diff. In this way we can directly turn this output into a Patch:
sweetdum@sweetdum-ASUS:~/GitEx$ git commit -a -m "Fix"
[Fix b88c46b] Fix
1 files changed, 1 insertions(+), 0 deletions(-)
sweetdum@sweetdum-ASUS:~/GitEx$ git diff master > patch
sweetdum@sweetdum-ASUS:~/GitEx$ git checkout master
Switched to branch 'master'

We now have a patch file and have master checked out. Next we can use git apply to apply this patch. Of course, in actual application, we will not build a patch in one branch and apply it in another branch, because we only need to merge it. We are now There is no such Fix branch. Under normal circumstances, in order to protect the master, we will establish a branch specifically to handle newly submitted patches:

sweetdum@sweetdum-ASUS:~/GitEx$ git branch PATCH
sweetdum@sweetdum-ASUS:~/GitEx$ git checkout PATCH
Switched to branch 'PATCH'
sweetdum@sweetdum-ASUS:~/GitEx$ git apply patch
sweetdum@sweetdum-ASUS:~/GitEx$ git commit -a -m "Patch Apply"
[PATCH 9740af8] Patch Apply
1 files changed, 1 insertions(+), 0 deletions(-)

Look, now that we have applied this patch in the PATCH branch, we can compare the PATCH branch with the Fix branch. The result must be nothing, indicating that the PATCH branch and the Fix branch are exactly the same. The patch is applied successfully. Even if there are multiple files git diff can also generate a patch.

2.git-specific patch generated by git format-patch.

We also use the working directory of the above example. This time, after adding a new line to a in the Fix branch, we use git format-patch generates a patch.
sweetdum@sweetdum-ASUS:~/GitEx$ git checkout Fix
Switched to branch 'Fix'
sweetdum@sweetdum-ASUS:~/GitEx$ echo 'Fix!!!'>>a
sweetdum@sweetdum-ASUS:~/GitEx$ git commit -a -m "Fix1"
[Fix 6991743] Fix1
1 files changed, 1 insertions(+), 0 deletions(-)
sweetdum@sweetdum-ASUS:~/GitEx$ git format-patch -M master
0001-Fix1.patch

The -M option of git format-patch indicates that this patch should be compared with that branch. Now it generates a patch file, let’s see what it is:

sweetdum@sweetdum-ASUS:~/GitEx$ cat 0001-Fix1.patch
From 6991743354857c9a6909a253e859e886165b0d90 Mon Sep 17 00:00:00 2001
From: Sweetdumplings
Date: Mon, 29 Aug 2011 14:06:12 +0800
Subject: [PATCH] Fix1

---
a | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/a b/a
index 4add65f. .0d295ac 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
This is the file a.
+Fix!!!
--
1.7.4.1

Look, there are a lot more things this time, not only diff information , as well as the submitter, time, etc. If you look closely, you will find that this is an E-mail file, you can send it directly! For this kind of patch, we need to use git am to apply.

sweetdum@sweetdum-ASUS:~/GitEx$ git checkout master
Switched to branch 'master'
sweetdum@sweetdum-ASUS:~/GitEx$ git branch PATCH
sweetdum@sweetdum-ASUS:~/GitEx$ git checkout PATCH
sweetdum@sweetdum-ASUS:~/GitEx$ git am 0001-Fix1.patch
Applying: Fix1
sweetdum@sweetdum-ASUS:~/GitEx$ git commit -a -m "PATCH apply"

After submitting the patch, we can take a look at the current situation of file a:

sweetdum@sweetdum-ASUS:~ /GitEx$ cat a
This is the file a.
Fix!!!

Sure enough, there is one more Fix!!!

But it should be noted that if there are multiple commits between the master and Fix branches, it will Each submission generates a patch.

3. Comparison of the two patches:

Compatibility: Obviously, the patch generated by git diff has strong compatibility. If the official repository of the code you are modifying is not a Git-managed repository, then you must use git The patch generated by diff can make your code accepted by the project maintainer.

Debugging function: For the patch generated by git diff, you can use git apply --check to see whether the patch can be applied cleanly and smoothly to the current branch; if git The patch generated by format-patch cannot be applied to the current branch. git am will give a prompt and assist you in completing the patching work. You can also use git am -3 Perform a three-party merge. For detailed methods, please refer to the git manual or "Progit". From this point of view, both debugging capabilities are very strong.

Repository information: thanks to git The patch generated by format-patch contains the name of the patch developer, so when the patch is applied, this name will be recorded in the repository. Obviously, this is appropriate. Therefore, open source communities currently using Git often recommend that everyone use format-patch to generate patches.


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