Patching with Git

June 27, 2008

Git is hard; but powerful. A wee bit of command porn for you for contributing with open source projects.

When sitting in a branch you have been working on for a new feature / bugfix which was created with the command:

git checkout -b cool_patch

To create a patch for the master branch which is a remote tracking branch for a read only git repo. Typically, this is an open source project which you cant push to therefore you need to patch.

git format-patch master --stdout > ~/patches/patchname.diff

Now you can create a ticket on the open source project with your patch.

||

To load a patch, its good to do it in a new branch.

So a workflow to applying a patch is as follows

git branch master
  git checkout -b new_patch
  cat ~/patches/patchname.diff | git am

You can then check you are happy and when you are. You can merge back into master by committing the branch changes then switching back to the master branch and merging your

git add .
  git commit -a -v 
  git branch master
  git merge new_patch

Please correct me on any glaring errors you see. Im pretty much making it up as I go along.