Recent Changes - Search:

Basics

Languages

Tools

OS

Resources

PmWiki

pmwiki.org

edit SideBar

Git /

Patching

Patching is the act of applying a modification to the code from an external source from which the current code is not necessarily related. Where the normal work flow builds upon the existing code and a merging branches combines the history of two branches into one, a patch is not originally a part of the repository and usually comes from an exterior source.

Creating Patches

  • This will create 1 patch for the commits between those specified
git checkout "repo/branch of a patch"
git format-patch "reference commit".."last commit of a patch"

Alternatively, you can create a patch by naming one [tt]reference commit[/tt] only, which will take all the changes from then to the latest commit.

  • To get 1 patch file per commit
git checkout "repo/branch of patch"
git checkout "last commit of a patch"
git format-patch "reference commit"
  • This will create 1 patch file total
git checkout "repo/branch of patch"
git checkout "last commit of a patch"
git format-patch "reference commit" --stdout > "Patch-File".patch
  • To create patch files only pertaining to certain files and put them in a target directory:

git format-patch -o "path/to/patch/directory" "reference commit" "final commit" -- "path/to/file(s)/to/track"

NOTE: It might be possible to create a patch by using: git diff "Last" "Ref" > Patch
NOTE 2: git log -p, although it creates something very similar to a patch file, generates it in the wrong order (last commit at the top), meaning that git is not able to actually apply it.

Applying Patches

Applying a patch is a simple thing, provided there is no conflict emerging from it. I usually do:

git am < PatchToBeApplied

Where am stands for "Apply Mail" since git patches take the form of an email, the < is the bash redirection input operator and PatchToBeApplied is a patch file, as created above.

To the target version

If the patch was written for a commit that exists in the history of your repository, you have an easier case, for which you can:

  1. Checkout the relevant commit? from your master or other desired branch
  2. Create a new branch at that commit
  3. Apply the patch to the commit
  4. Merge the new branch into your master or other desired branch

To a different version

Edit - History - Print - Recent Changes - Search
Page last modified on October 26, 2017, at 04:36 PM