The difference between patch and diff

Git provides two patch schemes: one is the UNIX standard patch. Diff file generated by Git diff, and the other is the git-specific. Patch file generated by Git formate-patch. A. Diff file only records changes in the file and does not contain commit information. Multiple COMMITS can be merged into a SINGLE DIff file. The. Patch file records the changes in the file and also contains the commit information. Each COMMIT corresponds to a patch file.

In Git, we can use. Diff file or. Patch file to patch. The main application scenarios are: CodeReview, code migration, etc.

Create patch and diff

1. Common command lines for creating patch files

* Several submissions prior to a submission (including) :
Git format-patch [commit sha1 id] -nCopy the code

N indicates the number of commits counted from the commit corresponding to sha1 ID. Eg:

git format-patch  2a2fb4539925bfa4a141fe492d9828d030f7c8a8 -2
Copy the code
* A committed patch:
Git format-patch [commit sha1 id] -1Copy the code

Eg:

git format-patch  2a2fb4539925bfa4a141fe492d9828d030f7c8a8 -1
Copy the code
* All patches between a certain commit:
Git format-patch [commit sha1 id].. 【commit sha1 id】Copy the code

Eg:

git format-patch 2a2fb4539925bfa4a141fe492d9828d030f7c8a8.. 89aebfcc73bdac8054be1a242598610d8ed5f3c8Copy the code
2. Common methods for creating diff files
Using the command line
Git diff [commit sha1 id] [commit sha1 id] >Copy the code

Eg:

git diff  2a2fb4539925bfa4a141fe492d9828d030f7c8a8  89aebfcc73bdac8054be1a242598610d8ed5f3c8 > patch.diff
Copy the code
The use of SourceTree

Select the target commit, right click, and select Create Patch

3. How do I obtain the COMMIT SHA1 ID

Git sha1 id: Git sha1 id: Git sha1 ID

2a2fb4539925bfa4a141fe492d9828d030f7c8a8

If you use Sourcetree, right click on the commit and select Copy SHA-1 toclipboard to copy the SHA1 ID to the clipboard:

Use Patch and diff

Related Command Lines

Check whether patch/diff can be used normally:
Git apply --check [path/to/xxx.patch] git apply --check [path/to/xxx.diff]Copy the code
Into the patch/diff:
Git apply [path/to/xxx.patch] git apply [path/to/xxx.diff]Copy the code

or

Git am 【path/to/xxx.patch】Copy the code

The use of SourceTree

Select SourceTree and select Aciotn-Apply Patch at the top of the screen

Iv. Conflict resolution

In the process of patch installation, sometimes there will be conflicts. In case of conflicts, the entry will fail, as shown in the figure:

In this case, the conflict needs to be resolved: 1. First, use the following command line to automatically incorporate non-conflicting code changes in patch and keep the conflicting parts:

git  apply --reject  xxxx.patch
Copy the code

The contorted code can be displayed in the terminal:

git add.
git am --resolved
git am --continue

Note: This command can be executed when entering the patch conflictgit am --skipSkip this conflict, you can also executegit am --abortRoll back the operation of inserting the patch to restore the state before the operation.

See Git AM Conflict Resolution for details on conflict resolution

Refer to the link

https://blog.csdn.net/liuhaomatou/article/details/54410361 https://blog.csdn.net/maybe_windleave/article/details/8703778 https://www.cnblogs.com/y041039/articles/2411600.html