Git's patch Feature

Source: Internet
Author: User

Software development in the UNIX world is mostly collaborative, so patches are a very important thing because almost all of the ordinary contributors to large UNIX projects are submitting code through patches. As one of the most important open source projects, Linux is also the case. Ordinary developers from the software repository clone under the code, and then write code, make a patch, and finally e-mail to the Linux kernel maintainer is good. Git was originally a Linux version control tool that provides transparent, complete, and stable patch functionality.

Let's start by introducing what patch is. If a new version of the software is available, we can download the new version of the code completely to compile and install it. However, for large projects such as Linux kernel, even if the code is compressed and more than 70MB, there is a significant price for each new download. However, the code for each update change may not exceed 1MB, so we should be able to update the program at a very low cost as long as we can have a diff of two version codes. As a result, Larry wall developed a tool: patch. It can be updated according to a diff file.

But in git, it's not necessary to use diff and patch directly to make patches, which is both dangerous and cumbersome. Git offers two simple patch scenarios. One is the standard patch generated with Git diff, and the other git-specific patch generated by Git format-patch.

Standard patch generated by 1.git diff

We can first make a patch with Git diff. The working directory for the sample in this article originally had a file a, which was "this is the file a.", placed in the master branch. In order to modify the code, it is common practice to 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 the a file and then execute git diff.
[Email protected]:~/gitex$ echo ' Fix!!! ' >>a
[Email protected]:~/gitex$ git diff
Diff--git A/a b/a
Index 4add65f: 0D295AC 100644
---a/a
+ + b/a
@@-1 +1,2 @@
The is the file a.
+fix!!!

We see the output of Git diff, which is a very typical patch diff. So we can just turn this output into a patch:
[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 '

We now have a patch file and checked out master, and then we can use git apply to apply the patch. Of course, in practice, we would not build patches in one branch and apply them to another, because it would be nice to merge only a bit. We are right now when there is no this fix branch. In general, to protect master, we create a branch that specializes in dealing with SGX patches:

[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 (-)

Look, now that we've applied this patch to the patch branch, we can pair the patch branch with fix, and there's definitely nothing left to show that the patch branch is exactly the same as the Fix branch. Patch application succeeded. Even if you have more than one file, git diff can generate a patch.

2.git Format-patch generated git-specific patches.

We also use the working directory of the example above, this time, after we add a new line to a in the Fix branch, we use Git format-patch to generate a patch.
[Email protected]:~/gitex$ git checkout Fix
Switched to branch ' Fix '
[Email protected]:~/gitex$ echo ' Fix!!! ' >>a
[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 is to be compared to that branch. Now that 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, 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 @@
The is the file a.
+fix!!!
--
1.7.4.1

See, this time a lot of things, not only have diff information, as well as the submitter, time and so on, carefully see you will find that this is an e-mail file, you can send it directly! This patch, we want to use git am to apply.

[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 has been submitted, we can look at the current status of file a:

[Email protected]:~/gitex$ Cat A
The is the file a.
Fix!!!

Sure enough, one more fix!!!

Note, however, that if there are multiple commits between the master and the Fix branch, it will generate a patch for each commit.

3. Comparison of two patch types:
    • Compatibility: It is obvious that Git diff generates a strong patch compatibility. If the official repository of the code you are modifying is not a git-managed repository, you must use Git diff-generated patches to make your code acceptable to the maintainer of the project.
    • Debug function: For git diff generated patches, you can use Git apply--check to see if the patch can be applied to the current branch cleanly and smoothly, if Git format-patch generates a patch that does not hit the current branch, git am will give a hint, and assist you with patching, you can also use Git am-3 for a three-way merger, and you can refer to the GIT manual or Progit for detailed instructions. From this point of view, the two functions are very strong apart.
    • Repository information: Since the patch generated by Git Format-patch contains the name of the patch developer, the name is recorded in the repository when the patch is applied, and it is clear that this is appropriate. As a result, the open source community that currently uses git often suggests that you use Format-patch to generate patches.
Category: Management

Git's patch Feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.