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.

  • Looking for more info from my Git diff command
  • Customizing headings in git diff
  • How to check real git diff before merging from remote branch?
  • What does “@@ -1 +1 @@” mean in Git's diff output?
  • Why doesn't `git diff` invoke external diff tool?
  • diff returning entire file for identical files
  • Bash git alias tab completion error
  • Git: How to clone the first commit?
  • Should I merge .pbxproj files with git using merge=union?
  • Any good options for hosted Git/Trac?
  • Why does this merge produce a conflict
  • Are there crucial differences between stashing, pulling & popping vs. commiting & pull --rebase?
  • One Solution collect form web for “Git/Diff Patch technical explanation”

    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.

    Git Baby is a git and github fan, let's start git clone.