Git's patch Feature

Source: Internet
Author: User
Tags diff using git

This article organizes edits from:

http://www.cnblogs.com/y041039/articles/2411600.htmlhttp://yuxu9710108.blog.163.com/blog/static/2375153420101114488765/ First, prefaceSoftware 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. second, git diff generated by the standard patch 2.1. Making Standard PatchesWe 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, our general practice is to create a new branch: [email protected]:~/gitex$ git branch fix[email protected]:~/gitex$ git checkout fixswitched to Branch ' Fix ' next we append a line to the a file and then execute git diff. Swee[email protected]:~/gitex$ echo ' Fix!!! ' >>a[email protected]:~/gitex$ git diffdiff--git a/a b/aindex 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 diff. So we can just turn this output into a patch:[email protected]:~/gitex$ git commit-a-M "Fix" [fix b88c46b] Fix1 files changed, 1 insertions (+), 0 deletions (-) [Email protected]:~/gitex$ git diff master > patch[email protected]:~/gitex$ git checkout masterswitched To branch ' master ' 2.2. Apply Standard patchesWe 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, in order to protect master, we will create a branch called patch to specifically handle the newly submitted patch [email protected]:~/gitex$ git branch patch[email protected]:~/ gitex$ git checkout patchswitched to branch ' PATCH ' [email protected]:~/gitex$ git apply patch[email protected]:~/gitex$ gi T commit-a-M "Patch Apply" [Patch 9740af8] patch Apply1 files changed, 1 insertions (+), 0 deletions (-) Look, now we've applied this in the Patch branch Patch, we can compare patch branch to fix, the result must be nothing, it means the patch branch and fix branch are exactly the same. Patch application succeeded. Even if you have more than one file, git diff can generate a patch. Git format-patch generated git-specific patches 3.1, make git special patchWe 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 fixswitched to Branch ' Fix ' [email protected]:~/ Gitex$ echo ' Fix!!! ' >>a[email protected]:~/GitEx$ git commit-a -m  "Fix1" [Fix 6991743] Fix11 files Changed, 1 insertions (+), 0 deletions (-) [email protected]:~/gitex$ git format-patch -m  The Master0001-fix1.patchgit format-patch-m option indicates that the current branch of the patch is in alignment with that branch. Now that it generates a patch file, let's see what it is: [Email protected]:~/gitex$ cat 0001-fix1.patchfrom 6991743354857c9a6909a253e859e886165b0d90 Mon Sep 00:00:00 2001from:sweetdumplings <[email protected]> Date:mon, 14:06:12 +0800subject: [PATCH] Fix1---A |    1 +1 files changed, 1 insertions (+), 0 deletions (-) diff--git a/a B/aindex 4add65f. 0D295AC 100644---a/a+++ b/a@@-1 +1,2 @ @This is the file A.+fix!!! --1.7.4.1 See, this time a lot of things, not only have diff information, there are submitter, time and so on, carefully see you will find, this is an e-mail file, you canSend it directly! Note: If you have multiple commits between master and the Fix branch, it will generate a patch for each commit. 3.2. Apply git-specific patchesGit format-patch generates patches that must be applied using the git AM command. [Email protected]:~/gitex$ git checkout masterswitched to branch ' master ' [email protected]:~/gitex$ git branch Patch[emai L protected]:~/gitex$ git checkout patch[email protected]:~/gitex$ git am 0001-fix1.patchapplying:fix1[email protected] : ~/gitex$ git commit-a-M "patch apply" after the patch has been submitted, we can look at the current situation of file a: [email protected]:~/gitex$ cat AThis is the file a.fix!! Sure enough, one more fix!!! 3.3. Apply multiple Git patchesBecause Git is used, there are a lot of times when someone (a vendor or other developer) sends a series of patches that are typically similar to a name: Git-am can merge one file at a time, or all patches in a directory, or patch in your mailbox directory. Before using Git-am, you have to first git am–abort once to discard the previous AM information so that you can make a new am. Otherwise you will encounter this error: Git/rebase-apply still exists but mbox given. Here are two examples. Example 1You now have a code BASE:SMALL-SRC, your patch file is placed in ~/PATCH/0001-TRIVAL-PATCH.PATCHCD small-srcgit am ~/patch/0001- Trival-patch.patch if the patch is successful, you can go and have a cup of tea. If you fail, Git will prompt you for an error, such as: Error:patch Failed:android/mediascanner.cpp:452error:android/mediascanner.cpp:patch does not app ly so you need to look at the patch first and then change the wrong file so that the patch can patch up. Example 2 you have a bunch of patches, the names of the patches mentioned above, you put them in the ~/patch-set/directory (path free) CD opencoregit am ~/patch-set/*.patch ( Here git will follow the file name in the order of the AM these patches) if all goes well, all your patches are OK, and you lucky again. 3.4. Application of git special patch failedWhen using git am patching, if you encounter a patch failure in the middle, then Git am will stop at the patch and tell you which patch is not up. For example, I now have a file, there are two patch.file content is the Text more text two patches are: 0001-add-line.patch patch file: From 48869ccbced494e05738090afa5a54f2a261df0f Mon Sep 00:00:00 2001from:zhangjiejing <[email protected] (none) >date:thu, April 13:04:34 +0800subject: [PATCH] Add line --- file |    2 ++ 1 files changed, 2 insertions (+), 0 deletions (-)  diff--git a/file b/fileindex 067780e.. 685F0FA 100644---a/file+++ b/file@@ -3,3 +3,5 @@ file: some text  more text++add Line--1.6.3.30002-change-line.patch patch file: From f756e1b3a87c216b7e0afea9d15badd033171578 Mon Sep 00:00:00 2001From: Zhangjiejing <[email protected] (none) >date:thu, April 13:05:19 +0800subject: [PATCH 2/2] Change LINE&NB SP;--- file |    2 +- 1 files changed, 1 insertions (+), 1 deletions (-)  diff--git a/file b/fileindex 685f0fa. 7af7852 100644---a/file+++ b/file@@ -1,6 +1,6 @@ file: -some text+change line text  more text --1.6.3.3 run git am *. Patch to apply these patches, error,  patch failed at 0001 add line so we look at 0001 this patch, the original patch needs to be some text, and file is the text,& nbsp; So we use the editor to change this line to some Text,vi filegit apply 0001-add-line.patchgit add filegit am--resolved after the conflict is resolved, such as with Git Add to let git know that you've resolved the conflict. If you find this conflict to be unresolved, undo the whole AM thing. You can run Git am–abort, and if you want to just ignore this patch, you can run Git am–skip to skip the patch. Iv. Comparison of two patches compatibility: Obviously, 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. error-removing function: For a 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 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. Version Library 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.

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.