简体   繁体   中英

Git/Diff Patch technical explanation

I was experimenting using git format-patch to create patches from one repo and applying it to another repo. To my surprise it worked even though those two files were extremely diff. Can someone explain the technicals on how exactly git handles applying a patch? Obviously it doesn't just use line numbers, so if someone can point me in the right direction that would be awesome.

There are two main ways patches can be applied to modified files:

  • matching unmodified (“context”) and pre-modification lines
  • 3-way merge based on Git's “index” line

The context lines (preceded by a single space instead of a + or - ) are a part of the unified diff format upon which Git's diff format is largely based. They are extra lines that are identical in the “original” and “modified” source files but that that surround the modified regions. These context lines (along with the pre-modification lines (ie deleted/changed lines)) are used by the program that applies the diff to find where each diff “hunk” should be applied even if the destination “target” file has already inserted or removed lines before the normal target location (the normal location is specified by line numbers, which have effectively changed in the “target” file (with respect to the “original” file) because of the inserted/removed lines).

Git's diff format also includes a special “index” line for each modified file that indicate the object ids (ie abbreviated SHA-1 hashes) for the “original” and “modified” files. If the destination repository has the “original” file in its object store, it can use it to exactly reconstruct the content of the “modified” file and then perform a three-way merge between the three versions of the file: “original”, “modified” (both source files) and “target” (the destination file). This is used by git am -3 and can help automatically resolve some conflicts between the patch and the “target” file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM