Emailing Git Commits

Did you know you can use Git to submit patches to projects via email (or website)? Thanks to Bruce I not only found this out but I also found out how to merge the patches as well. In this post I’m going to show you how to do it. There are quite a few uses for this, for example you might want to use this method for submitting patches to a bug tracker or to allow someone to contribute without giving them direct commit access to the repository.

Firstly you’ll need to grab yourself a copy of the repo for the project you want to write a patch for. This is a pretty standard thing to do.

$ git clone git://github.com/schacon/simplegit.git

Next you make your changes and commit them to your repository, again something standard for git users.

$ git commit -am "added foo to component bar"

Now the interesting bit, we’re going to generate a series of patch files we can send to the owner of the repository. The following command will take all commits which are not in origin (the place you got the repo from), with one commit per file.

$ git format-patch

You can then attach these patch files to a bug tracker, email or whatever you like using a version control. There is also a git command to send email but I’ve never used it so I can’t comment on it’s use. It’s git-send-email.

On the other side of equation there is merging of the patches. There are multiple ways to do this and a few useful commands related to it. The first command tells you how much how much the patch changes. Check checks if the patch can be applied cleanly. If the patch can be applied then you can use either ‘git apply’ or ‘git am’ to apply the patch.

If you use git am you can supply –signoff to let people know that the patch is approved. You can also supply -3 if the patch fails it will allow you to merge changes and use git  am  –resolved to mark the merge as resolved.

$ git apply --stat my_patch.patch
$ git apply --check my_patch.patch
$ git am -3 --signoff < my_patch.patch 
$ git am --resolved # if the patch needs merging to take place

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.