Git patch tutorial

I had some patches in one Git repository that I wanted to apply to a different repository. Here's a useful link I found on this :

http://stackoverflow.com/questions/327249/seeking-examples-of-workflow-u...

Here's what I did.

First, copy the original repository you want to get patch from. This may not be necessary in all cases, but in my case the original repository was on another machine, so this just puts everything on the same machine.

  
    $ git clone ssh://me@example.com/repos/origRepo
    $ cd origRepo

Make a directory to hold the patches you'll make.

  
    $ mkdir /tmp/myPatches 

In the original repository, identify the patch(es) you want to apply. I found this out with gitk (git log may also work). In my case, it was just one commit, so I just put in the two commit hashes which indicate the beginning and ending of the patch :

  
    $ git format-patch -M -C --output-directory /tmp/myPatches 17fbf896..fb9bea1 

Now inspect your new patch. Note how the patch is named after the commit message :

  
    $ less /tmp/myPatches/0001-DKL-mods-to-theme.patch 

Now go to the 'new' repository which accept the patch :

  
    $ cd /repos/newRepo 

Make a new, temporary branch to accept your patch. You'll apply the patch to the new branch. If things look good, you can merge in the newly applied patch.

  
    $ git checkout myBranch 
    $ git checkout -b myBranch-patches 

Apply the patch to your new, temporary branch

  
    $ git am /tmp/myPatches/00* 

See what the patch gave you :

  
    $ git diff --stat myBranch  myBranch-patches 
    $ git difftool myBranch  myBranch-patches 

If things look good, merge it into the original branch :

  
    $ git checkout myBranch
    $ git merge myBranch-patches
Categories: 

There is 1 Comment

Very useful! Thanks.
I just wanted to mention that the git format-patch command should be as follows:
$ git format-patch -M -C --output-directory /tmp/myPatches 17fbf896...fb9bea1