How to use Git to make and submit patch

Source: Internet
Author: User
Tags diff how to use git using git
software Development in the UNIX world is mostly collaborative, so Patch (patch) is a pretty important thing because almost all the common contributors to large UNIX projects are submitting their code through Patch. As one of the most important open source projects, Linux is the same. Ordinary developers from the software warehouse clone code, and then write code, to do a patch, and finally e-mail to the Linux kernel defender. Git was originally a Linux version control tool, providing 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 completely download the new version of the code to compile the installation. However, large projects such as Linux kernel, the code even compressed, also more than 70MB, each new download is a considerable price. However, the code for each update change may be no more than 1MB, so we should be able to update the program at a very low cost as long as we can have a diff data with two versions of the code. So Larry Wall developed a tool: patch. It can be versioned according to a diff file.

But in git, we don't need to use diff and patch directly to make patches, which is both dangerous and troublesome. Git offers two simple patch scenarios. One is the standard patch generated with Git diff, and the second is git format-patch generated by git dedicated patch. 1.git diff-generated standard patch

We can start with Git diff to make a patch. The working directory for the example in this article originally had a file a, which was "this is the file a." and placed in the Master branch. In order to modify the code, our general practice 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 the a file and 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 see the output of Git diff, which is a very typical patch-style diff. So we can turn this output directly 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 check out master, and then we can use git apply to apply this patch. Of course, in practical applications, we don't build patch in one branch and apply it to another, because only the merge is good. We don't have this fix branch right now. In general, to protect master, we will create a branch that deals specifically with the SGX patch:

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've applied this patch in the patch branch, we can compare the patch branch to fix, and the result is definitely nothing, which means the patch branch is exactly the same as the Fix branch. Patch applied successfully. Even if you have multiple files git diff can generate a patch. 2.git Format-patch generated git special patches.

We also use the working directory for the example above, and this time we build a patch with Git format-patch after we add a new line to a in the Fix branch.
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 for 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 that is:

sweetdum@sweetdum-asus:~/gitex$ Cat 0001-fix1.patch
From 6991743354857c9a6909a253e859e886165b0d90 Mon Sep 17 00:00:00 2001
From:sweetdumplings <linmx0130@163.com>
Date:mon, 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, this time a lot of things, not only have a diff information, there are the submitter, time and so on, a closer look you will find that this is an e-mail file, you can send it directly. This patch, we're going 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 the patch has been submitted, we can take a look at the current file a:

sweetdum@sweetdum-asus:~/gitex$ Cat A
This is the file a.
Fix!!!

Sure enough, one more fix!!!

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

Submit patches to Mailing list:

sweetdum@sweetdum-asus:~/gitex$ git send-email--to <maintainer ' s email>--cc <mailing list> <your Gt

Note: Git send-email is not installed by default on many machines and requires a manual installation

To summarize, using Git format-patch to make a patch may have the following steps: 1 new branch  2) make related modifications on the new branch  3) commit modifications to local database   4 to generate patch 5 for the last support) another new branch, Verify patch 6 Submit patch to mailing list 3. Two patch  comparisons: compatibility: It's obvious that git diff generates patch compatibility. If the official version of the code you are modifying is not a repository of git-managed versions, you must use Git diff-generated patch to allow your code to be accepted by the project's maintainers. Debugging Features: For git diff generated patch, you can use Git apply--check to see if the patch can be applied cleanly and smoothly to the current branch, and if Git format-patch generates a patch that doesn't hit the current branch, git am will give you a hint. and assist you to complete the patching work, you can also use Git am-3 to carry on the tripartite merge, the detailed procedure may refer to git manual or "progit". From this point of view, both the debugging function is very strong. Version Library information: Since the patch created by Git Format-patch contains the name of the patch developer, the name will be recorded in the version library when the patch is applied, which is clearly appropriate. Therefore, the open source community, which currently uses git, often advises you to use Format-patch to generate patches.

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.