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.

  • git diff shows unicode symbols in angle brackets
  • Git merge - manual merge - forcing conflic having WHOLE old and new file version
  • git compare commit ID between branches
  • How do I show only commits that have differences with git?
  • Strange output of git diff to file in Ubuntu
  • What does a red text-background mean in GitHub comparison view?
  • git: versioning a GNU/Linux distribution
  • Setup Git-User in Aptana
  • Submodule triggers a build
  • Configure Visual Studio to use system-installed Git.exe
  • What are scenarios in which 'git stash' might fail?
  • Installing Git 1.9.5 on Windows 10
  • 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.