git and patches
Sometimes you want to send or receive patches of work done. I found this great tutorial on how you can do that using git, which I will briefly rewrite.
How to create patches using git?
I assume you have cloned the original repository, and created five commits that you want to send back to one of the developers. My favourite way to do that is by
git format-patch HEAD~5
The command will create five separate patch files, which you can attach to an e-mail and send. The ~5 means "the fifth commit before", in other words, the sixth last commit. That would be the last commit that are already in upstream, which you are not interested in. Assuming the work is done on the master branch and the remote repository is called origin, you could also do
git format-patch origin/master
which will do the same, create one patch file per commit since the commit which is already in origin/master.
How to receive patches?
The patches includes a header which contains the commit message, and author information. Normally you want to keep that. The initial thought is probably to use
git apply file.patch
However, this will only apply the changes, not commit them. Instead, one should first use the apply command to check that the patch is OK:
git apply --check file.patch
If you do not receive an error, it means that the patch will apply without failing. The way to apply it then is using
git am --signoff file.patch
This will commit the patch with original author information, and append to the commit message that it was signed off by you. The patches are numbered, starting with 0001. After you have committed all the patches to your own tree, you should make sure that they are in fact correct. If you find problems, you can always use rebase to remove commits again.
git rebase -i HEAD~5
will open an editor where you can select commits that you want to change. You can delete commits simply by deleting the corresponding line.
That’s it, now you can in an efficient and controlled way distribute and receive patches even when the repositories are not stored in a publicly available place!

Hi! The only tutorial that really helped me..One question though: If you apply the last patch, does this mean that all the previous changes(in the other 4) are also included or you have to apply each one sequentially ?
Hi FiL,
If you only apply the last patch you will only apply that one. If that one depends on changes from another patch then it will most likely fail, something you will notice with the “git apply –check” command.