In Git, there are two ways to patch. One is a DIff file, which generates Unix standard patches, and the other is a patch file, which generates Git-specific patches.

  • Diff only generates changes to files, but does not record commit information. Multiple Commits can combine a DIFF file
  • Patch not only records file change information, but also commit information, such as commiter, commit date, etc. Each commit will generate a patch file separately (because it contains commit information).

The diff file

diff --git a/a.txt b/a.txt                                                                                                                                                                                  
index 52295a4.. 469cdef 100644
--- a/a.txt
+++ b/a.txt@@-1 +1,3 @@Wo Shi Lightingsui+
+wo yao shengcheng diff wenjian
Copy the code

Patch file

From 275803519accd2b4e89e20803ccc1814b8154c18 Mon Sep 17 00:00:00 2001
From: coolsui <[email protected]>
Date: Thu, 24 Feb 2022 10:46:57 +0800
Subject: [PATCH] edit diff file

Signed-off-by: coolsui <[email protected]>
---
 a.txt    | 2 ++
 sui.diff | 0
 2 files changed, 2 insertions(+)
 create mode 100644 sui.diff

diff --git a/a.txt b/a.txt
index 52295a4.. 469cdef 100644
--- a/a.txt
+++ b/a.txt@@-1 +1,3 @@Wo Shi Lightingsui+
+wo yao shengcheng diff wenjian
diff --git a/sui.diff b/sui.diff
new file mode 100644
index 0000000.. e69de29
-- 
2.17.1
Copy the code

Generate the patch

There are now three commits, as follows

commit c3c74a0c4a1237a0f0f80b33c4d17dd13538789d (HEAD -> sui)
Author: coolsui <[email protected]>
Date:   Thu Feb 24 15:08:07 2022 +0800

    switch sui branch and edit a.txt

diff --git a/a.txt b/a.txt
index 469cdef.. 7d1ad46 100644
--- a/a.txt
+++ b/a.txt
@ @ - 1, 3 + 1, 5 @ @
 wo shi lightingsui
 
 wo yao shengcheng diff wenjian
+
+wo switch sui branch

commit 8472d9ba2fbb57ea8f2fd84290883ff55ba2a7a6
Author: coolsui <[email protected]>
Date:   Thu Feb 24 11:09:50 2022 +0800

    edit a.txt
    
    Signed-off-by: coolsui <[email protected]>

diff --git a/a.txt b/a.txt
index 52295a4.. 469cdef 100644
--- a/a.txt
+++ b/a.txt@@-1 +1,3 @@Wo Shi Lightingsui+
+wo yao shengcheng diff wenjian

commit d3d623ff40dcfc2ef07c2499f2d20a05b6c16a8a
Author: coolsui <[email protected]>
Date:   Thu Feb 24 10:44:21 2022 +0800

    add a.txt
    
    Signed-off-by: coolsui <[email protected]>

diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000.. 52295a4
--- /dev/null
+++ b/a.txt@ @ @ @ - 0, 0 + 1+wo shi lightingsui

Copy the code

Generate a DIFF patch

Git diff sha1 sha2 > diffName

The diff c3c74a0c4a1237a0f0f80b33c4d17dd13538789d and 8472 d9ba2fbb57ea8f2fd84290883ff55ba2a7a6 resulting from the file

git diff d3d623ff40dcfc2ef07c2499f2d20a05b6c16a8a c3c74a0c4a1237a0f0f80b33c4d17dd13538789d > 0001.diff
Copy the code
diff --git a/a.txt b/a.txt                                                                                                                                                                                  
index 52295a4.. 7d1ad46 100644
--- a/a.txt
+++ b/a.txt@@-1 +1,5 @@Wo Shi Lightingsui+
+wo yao shengcheng diff wenjian
+
+wo switch sui branch
Copy the code

Sha1 is the commit for the left interval, sha2 is the commit for the right interval, and the range of diff files (sha1, sha2)

Generating a patch

For the above c3c74a0c4a1237a0f0f80b33c4d17dd13538789d generated patch

Git format: -patch sha -n

N indicates that n patches (including the current commit) are applied to the commit represented by sha. If the value is 1, only patches are applied to the commit represented by SHA

sui@home:~/me/tmp/test$ git format-patch c3c74a0c4a1237a0f0f80b33c4d17dd13538789d -1
0001-switch-sui-branch-and-edit-a.txt.patch
Copy the code
From c3c74a0c4a1237a0f0f80b33c4d17dd13538789d Mon Sep 17 00:00:00 2001                                                                                                                                      
From: coolsui <[email protected]>
Date: Thu, 24 Feb 2022 15:08:07 +0800
Subject: [PATCH] switch sui branch and edit a.txt

---
 a.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/a.txt b/a.txt
index 469cdef.. 7d1ad46 100644
--- a/a.txt
+++ b/a.txt
@ @ - 1, 3 + 1, 5 @ @
 wo shi lightingsui

 wo yao shengcheng diff wenjian
+
+wo switch sui branch
-- 
2.17.1
Copy the code

The patch file not only generates DIff information, but also generates COMMIT information.

When n is greater than 1, multiple patch files are generated, and one file is generated for each COMMIT

sui@home:~/me/tmp/test$ git format-patch c3c74a0c4a1237a0f0f80b33c4d17dd13538789d -2
0001-edit-a.txt.patch
0002-switch-sui-branch-and-edit-a.txt.patch
Copy the code

The file is named in the following format: patch Autoincrement number -commit title.txt.patch

Apply patch

Both patch and DIff files can be applied, such as code transfer operations

git apply

The command format is git apply filename

No collision in

If no output is displayed, the check is passed
sui@home:~/me/tmp/test$ git apply --check 0001.diff

# application diff
sui@home:~/me/tmp/test$ git apply 0001.diff
# There is no output proof that the diff code is complete
sui@home:~/me/tmp/test$ 

Copy the code

Applying diff files or patch files using Apply will not automatically commit. This can be seen using Git Status (the modifications in the diff file you just applied now have not staged modifications)

sui@home:~/me/tmp/test$ git status
On branch sui
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   a.txt
Copy the code

conflict

# check failed
sui@home:~/me/tmp/test$ git apply --check 0001.diff
error: patch failed: a.txt:1
error: a.txt: patch does not apply

Failed to join
sui@home:~/me/tmp/test$ git apply 0001.diff
error: patch failed: a.txt:1
error: a.txt: patch does not apply

Copy the code

If a file in the REPo conflicts with the diff, the file cannot be closed, but the details of the conflict are not displayed (only the location of the conflict is displayed in the A.txt file).

You can use git apply –reject filename, which generates a.rej file named after the conflicting file that displays the conflicting information

sui@home:~/me/tmp/test$ git apply --reject  0001-edit-a.txt.patch
Checking patch a.txt...
error: while searching for:
wo shi lightingsui

error: patch failed: a.txt:1
Applying patch a.txt with 1 reject...
Rejected hunk # 1.

sui@home:~/me/tmp/test$ ls
0001-edit-a.txt.patch  0002-switch-sui-branch-and-edit-a.txt.patch  a.txt  a.txt.rej
Copy the code
sui@home:~/me/tmp/test$cat a.twt. Rej diff a/ a.twt b/ a.twt (Rejected Hunks) @@-1 +1,3 @@wo Shi lightingsui + + Wo Yao Shengcheng Diff wenjianCopy the code

Delete the rej information and commit according to the information of the conflicting files

git am

The command format is git am filename

No collision in

sui@home:~/me/tmp/test$ ls
0001-edit-a.txt.patch  0002-switch-sui-branch-and-edit-a.txt.patch  a.txt

#Batch adding patches indicates two COMMIT operations
sui@home:~/me/tmp/test$ git am *.patch
Applying: edit a.txt
Applying: switch sui branch and edit a.txt
Copy the code

Because the patch file carries commit information, you do not need to manually commit the patch file

sui@home:~/me/tmp/test$ git log
commit e0c28f3d46940580cb42cc981f13327969169a1b (HEAD -> sui)
Author: coolsui <[email protected]>
Date:   Thu Feb 24 15:08:07 2022 +0800

    switch sui branch and edit a.txt

commit 3ff2070a73b5ade9262f5bace7ee2ae373c048b5
Author: coolsui <[email protected]>
Date:   Thu Feb 24 11:09:50 2022 +0800

    edit a.txt
    
    Signed-off-by: coolsui <[email protected]>

commit d3d623ff40dcfc2ef07c2499f2d20a05b6c16a8a
Author: coolsui <[email protected]>
Date:   Thu Feb 24 10:44:21 2022 +0800

    add a.txt
    
    Signed-off-by: coolsui <[email protected]>
Copy the code

conflict

sui@home:~/me/tmp/test$ git am *.patch
Applying: edit a.txt
error: a.txt: does not match index
Patch failed at 0001 edit a.txt
Use 'git am --show-current-patch' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Copy the code

After a conflict occurs, provide three commands to handle the conflict

After handling the conflict, run the git add command to continue the merge operation
git am --continue

# skip conflicts
git am --skip

Return to the state before the path was applied
git am --abort
Copy the code

Apply is different from AM

  1. Git apply can merge git diff files and git format-patch files, while Git AM can merge git format-patch files
  2. Git apply does not commit. Git AM does commit