Most software development in the Unix world is collaborative, So patch is a very important thing, because almost all common contributors to large UNIX projects, all code is submitted through patches. As one of the most important open-source projects, Linux is also like this. General developers clone the code from the software warehouse, write the code, make a patch, and send it to the maintainer of Linux kernel via e-mail. As a Linux version control tool, git initially provides transparent, complete, and stable patch functions.
Let's first introduce what a patch is. If a software has a new version, we can download the new version of code for compilation and installation. However, for large projects such as Linux kernel, even if the code is compressed, it exceeds 70 mb. Each new download has a considerable cost. However, the code for each update change may not exceed 1 MB. Therefore, we only need to be able to have diff data for two versions of code, so we should be able to update the program at a very low cost. Therefore, Larry Wall developed a tool: patch. It can be updated based on a diff file.
However, in git, there is no need to directly use diff and patch for patching, which is both dangerous and troublesome. Git provides two simple patch solutions. One is the standard patch generated by git diff, and the other is the GIT dedicated patch generated by git format-patch.
1. Standard patch generated by git diff
We can use git diff to create a patch first. In the working directory of the example in this article, a file named "this is the file a." is first stored in the master branch. To modify the code, we generally create a new branch:
[Email protected]: ~ /Gitex $ git branch fix
[Email protected]: ~ /Gitex $ git checkout fix
Switched to branch 'fix'
Next we append a line to file a and execute git diff.
[Email protected]: ~ /Gitex $ echo 'fix !!! '>
[Email protected]: ~ /Gitex $ git diff
Diff -- git a/A B/
Index 4add65f .. 0d295ac 100644
--- A/
++ B/
@-1 + 1, 2 @@
This is the file.
+ Fix !!!
We can see the output of git diff, which is a very typical patch diff. In this way, we can directly change the output to a patch:
Sweetd [email protected]: ~ /Gitex $ git commit-a-m "fix"
[Fix b88c46b] fix
1 files changed, 1 insertions (+), 0 deletions (-)
[Email protected]: ~ /Gitex $ git diff master> patch
[Email protected]: ~ /Gitex $ git checkout master
Switched to branch 'master'
Now we have a patch file and checked out the master. Next we can use git apply to apply this patch. Of course, in practical applications, we will not create a patch in one branch to apply the patch to another branch, because only merge is enough. We do not have this fix branch. In general, to protect the master, we will create a branch dedicated to processing the new patch:
[Email protected]: ~ /Gitex $ git branch patch
[Email protected]: ~ /Gitex $ git checkout patch
Switched to branch 'patch'
[Email protected]: ~ /Gitex $ git apply patch
[Email protected]: ~ /Gitex $ git commit-a-m "patch apply"
[Patch 9740af8] patch apply
1 files changed, 1 insertions (+), 0 deletions (-)
Now we have applied this patch in the patch branch. We can compare the patch branch with the fix. The result is certainly nothing, indicating that the patch branch is exactly the same as the fix branch. Patch application successful. A patch can be generated even if multiple files are in git diff.
2. Git private patch generated by git format-patch.
We also use the working directory of the above example. This time, after a new line is added to fix branch a, a patch is generated using git format-patch.
[Email protected]: ~ /Gitex $ git checkout fix
Switched to branch 'fix'
[Email protected]: ~ /Gitex $ echo 'fix !!! '>
[Email protected]: ~ /Gitex $ git commit-a-m "fix1"
[Fix 6991743] fix1
1 files changed, 1 insertions (+), 0 deletions (-)
[Email protected]: ~ /Gitex $ git format-patch-M Master
0001-fix1.patch
The-M option of git format-patch indicates that the patch must be compared with that branch. Now it generates a patch file. Let's see what it is:
[Email protected]: ~ /Gitex $ cat 0001-fix1.patch
From 6991743354857c9a6909a253e859e886165b0d90 mon sep 17 00:00:00 2001
From: sweetdumplings <[email protected]>
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/
Index 4add65f .. 0d295ac 100644
--- A/
++ B/
@-1 + 1, 2 @@
This is the file.
+ Fix !!!
--
1.7.4.1
Look, there are a lot more things this time, not only the diff information, but also the submitter, time and so on. After a closer look, you will find that this is an e-mail file, you can directly send it! We need to apply this patch using git AM.
[Email protected]: ~ /Gitex $ git checkout master
Switched to branch 'master'
[Email protected]: ~ /Gitex $ git branch patch
[Email protected]: ~ /Gitex $ git checkout patch
[Email protected]: ~ /Gitex $ git am 0001-fix1.patch
Applying: fix1
[Email protected]: ~ /Gitex $ git commit-a-m "patch apply"
After the patch is submitted, let's look at the current file:
[Email protected]: ~ /Gitex $ cat
This is the file.
Fix !!!
Sure enough, one more Fix !!!
However, if the master and fix branches are submitted multiple times, a patch is generated for each commit.
3. Comparison of the two patches:
- Compatibility: Obviously, the patch generated by git diff is compatible. If the official version of the modified Code is not managed by git, you must use the patch generated by git diff to make your code accepted by the Project maintainer.
- Debugging function: for the patch generated by git diff, you can use git apply -- check to check whether the patch can be successfully applied to the current branch; if the patch generated by git format-patch cannot reach the current branch, git am will give a prompt and help you complete the patching. You can also use git am-3 for three-way merge, for details, refer to the GIT manual or progit. From this point of view, both of them have powerful debugging functions.
- Version Library Information: Because the patch generated by git format-patch contains the name of the patch developer, the name will be recorded in the version library when applying the patch. Obviously, this is appropriate. Therefore, we recommend that you use format-patch to generate patches for open-source communities using git.
Git patch Function