Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git/Diff Patch technical explanation

Tags:

git

diff

patch

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.

like image 463
David Avatar asked Mar 11 '11 00:03

David


People also ask

How does patch and diff work?

diff works by cataloging the changes between the two files or folders. Patch can take those changes, put them in a file, and update older versions with it.

What is git diff patch?

With the git diff command, you can create a record of how the file has changed, and the owner of a repository can use the patch command to "replay" those changes over the old version to bring it up to date with the new version.

How does a git patch work?

GIT patch or GIT diff is used to share the changes made by you to others without pushing it to main branch of the repository. This way other people can check your changes from the GIT patch file you made and suggest the necessary corrections.

How does git compute diffs?

Computing diffsThe diff is dynamically generated from the snapshot data by comparing the root trees of the commit and its parent. Git can compare any two snapshots in time, not just adjacent commits. To compare two commits, start by looking at their root trees, which are almost always different.


1 Answers

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 (i.e. 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 (i.e. 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.

like image 114
Chris Johnsen Avatar answered Sep 19 '22 23:09

Chris Johnsen