git apply, git am patching. diff and. Patch "Go"

Source: Internet
Author: User
Tags diff diff patch

This article was reproduced from: Https://www.jianshu.com/p/e5d801b936b6

Premise:

Generate Patch:
git format-patch-m master
generates the specified patch, 0163BED3BF59AE74C36CC5138B4C24F1556D8304 is a commit id,-1 is the start of the current ID, the number of commits down, including this and the count starting from 1.
that is, I want to 0163bed3bf59ae74c36cc5138b4c24f1556d8304 the current patch, then:
git format-patch 0163bed3bf59ae74c36cc5138b4c24f1556d8304-1
To make a patch that was committed before 0163bed3bf59ae74c36cc5138b4c24f1556d8304 and its previous commit:
git format-patch 0163bed3bf59ae74c36cc5138b4c24f1556d8304-2
generate diff:
git diff (id1) (ID2)-- Binary-(PATH) > destination file path
For example, to generate a diff under frameworks/base/, save to F_b.diff under ~/gittest/: (note: Old id1 in front)
git diff 206b47c132a80870c06d87c69a548bbfeebecd2d B5ce3e4ebe9503e370d734cecc12482bca023fdf-- Binary--frameworks/base/> ~/gittest/f_b.diff

Enter Patch/diff:
git apply xxx.patch
git apply xxx.diff
Check Patch/diff:
git apply --check xxx.patch
git apply --check xxx.diff
If git and the files that need to be patch are not in a directory: (Git under the framework, patch to break into frameworks/base/)
git apply --check --directory=base/ xxx.patch
git apply --directory=base/ xxx.patch
* * git AM will say later, as well as some command parameters for producing patches and breaking patches * *

    1. We create a folder, Git init, simulate diff/patch source environment
$ mkdir gittest$ git init
    1. Then create an empty file test, and then first commit
"test init"
    1. Add 11111,add,commit (add 11111) at once;
      Add 22222,add,commit (add 22222) at once;
      ...
      Add 55555,add,commit (add 55555) at once;
      Submitted altogether 5 times, you can see submitted 5 times.
GITK Generate Patch/diff files (we have a separate folder to store patches and diff--~/patch/patch/):

In the work, they choose one to see their own needs.
Patch relative to diff, more commit records, that is, can be left intact to the others commit content written up, but the operation than diff trouble some

    • * * Generate patch:**
      We recorded the latest commit ID:21EBFB1EF6A0A9B56D46036C036E8377B56B2DA5, which was submitted 5 times.
21ebfb1ef6a0a9b56d46036c036e8377b56b2da5 -5
Git patch

A patch containing the current ID and the previous 4 IDs has been generated, with a total of 5, named and regularly 000x+commit content. Patch
We move the 5 to the ~/patch/patch/.

    • * * Generate diff:**
      We record the interval to generate diff,!! It's not on the bag!

Which means I'm going to generate 11111~55555 diff.
ID1 is the id;1cf68afcf3e089a349c8ee534dc3ff44d11a6624 of test init.
ID2 is the ID of add 55555. 21ebfb1ef6a0a9b56d46036c036e8377b56b2da5

$ git diff 1cf68afcf3e089a349c8ee534dc3ff44d11a6624 21ebfb1ef6a0a9b56d46036c036e8377b56b2da5 --binary -- . > ~/patch/patch/test.diff

Diff multiple commits can be a file, but patch does not, because it has a commit record!

Hit Patch/diff patch (no conflict):

We create a patch-Required folder (GITTEST1) in the current directory, which also has an empty test file.
Then we put Patch/diff into gittest1/test.
Perform the first 1, 2, change the folder name is OK

Gittest1
    • Patch (does not contain commit content)
      1. Check if patch is available, no text is displayed, it is available, and there is no conflict;
        git apply --check ~/patch/patch/0001-add-11111.patch
        A general check is possible.
      2. You can break into patches, either in bulk or individually.
        git apply ~/patch/patch/*.patch
      3. Will certainly succeed, because there is no conflict. Let's take a look.
        git diff

Git diff
5 patches have been hit, and then you can git add / commitwas submitted. But this does not have a commit record.

    • make a patch (containing commit content git am ) undo it. git checkout .
      1. Check if patch is available, no text is displayed, it is available, and there is no conflict;
        git apply --check ~/patch/patch/0001-add-11111.patch
      2. You can break into patches, either in bulk or individually.
        ** git am ~/patch/patch/*.patch **
      3. Will certainly succeed, because there is no conflict. Let's take a look.

        gitk
Gitk

We found that the automatic commit, without add, commit, can be directly push.

    • * * Beat diff * * git reset --hard 4c6eb312e94214a5f34fa3f119382ace647b1b3c undo
      1. Check if diff is available, no text is displayed, it is available, and there is no conflict;
        git apply --check ~/patch/patch/test.diff
      2. Enter diff;
        git apply ~/patch/patch/test.diff
      3. Will certainly succeed, because there is no conflict. Let's take a look.
        git diff

Git diff
Then we can git add / commitwas submitted. But this does not have a commit record.

Hit Patch/diff patch (with conflict):

We restore the initial state of the gittest1/test, then modify the test file, write 00000, and then add, commit.

Add 00000
    • Hit patch
      1. Check if patch is available, no text is displayed, it is available, and there is no conflict;
        git apply --check ~/patch/patch/0001-add-11111.patch

Error: Patching failed: test:0
Error:test: Patch Not applied

说明是可以用,但是有冲突。  2. 打入patch,可以批量,也可以单个。> 因为在check的时候知道已经有冲突了,就不好用apply来打patch,如果一定要用的话,建议一个一个apply,所以很麻烦,不如用diff。在这里要用到 **git am**    ** `` git am ~/patch/patch/*.patch `` **

Positive application: Add 11111
Error: Patching failed: test:0
Error:test: Patch Not applied
Patch failed on 0001 add 11111
A copy of the failed patch file is located at:
/home/deshui/yudeshui/log/gittest1/.git/rebase-apply/patch
When you have resolved this issue, execute "git am--continue".
If you want to skip this patch, execute "git am--skip".
To restore the original branch and stop patching, execute "git am--abort".

 This jargon tells you that patch conflicts, but there are three options continue ", " git am--skip" git am--abort * * Don't move! Don't move! Don't move! * * 1. Resolve conflicts. > * * We  "gedit test 00000, resulting in the wrong line, so the next  4 patches will conflict. 2. Submit a record.  "git add test "  "git am--continue  "       

Positive application: Add 11111
Positive application: Add 22222
Error: Patching failed: test:1
Error:test: Patch Not applied
Patch failed on 0002 add 22222
A copy of the failed patch file is located at:
~/gittest1/.git/rebase-apply/patch
When you have resolved this issue, execute "git am--continue".
If you want to skip this patch, execute "git am--skip".
To restore the original branch and stop patching, execute "git am--abort".

我们``gitk``一下,发现已经commit已经有add 11111的记录了!** 同上操作,一直修改到44444 **     `` git add test `` `` git am --continue ``

Positive application: Add 44444
Positive application: Add 55555

这样就完整的解决冲突,保留commit了。![git am finish](http://upload-images.jianshu.io/upload_images/3153157-f2a6520f1ed25382.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)- **打diff ** ``git reset --hard 0050cda7f22df985d79b9b98da9bfc282ea10ef1 `` 撤销到add 00000 1. 检查diff是否可用,肯定有冲突。`` git apply --check ~/patch/patch/test.diff ``

Error: Patching failed: test:0
Error:test: Patch Not applied

  2. 打入diff;  `` git apply --reject --ignore-whitespace ~/patch/patch/test.diff 2>&1 | tee ~/patch/patch/testdiff.log``

Here reject is to generate a. rej file, which is a diff file.
Ignore-whitespace is to ignore extra spaces.
2>&1 is a value error message
Tee error message output to console
~/patch/patch/testdiff.log error message saved in this file
Output log**********
Check Patch test ...
Error: When querying:
Error: Patching failed: test:0
Apply test patch, 1 of which are rejected ...
Rejects the first #1 fragment.
Output log**********

  3. 这时候我们发现,文件夹多了一个test.rej的文件,打开它。

Diff A/test B/test (rejected hunks)
@@ -0,0 +1,5 @@
+11111
+22222
+33333
+44444
+55555

这个告诉我们,要在test中加入的信息,我们打开test补上就可以了。手动!然后add / commit 就可以了。###END
Git


Yotods
Links: Https://www.jianshu.com/p/e5d801b936b6
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

git apply, git am patching. diff and. Patch "Go"

Related Article

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.