Creating a patch file in Git

I don’t usually use patch files for their intended purpose (patching). I just don’t have the need for them in my development process. But, I do think the files themselves have another valuable purpose. They make a great way to do a quick code review of changes that were made.

So, whether you are actually applying a patch, or you just wish to review the changes between commits quickly, patch files are valuable.

If you wish to create a patch file with Git, it is a simple command…

git diff commit1 commit2 > nameOfPatch.patch

I will explain briefly what happened here… The “diff” command produces differences between commits. It basically creates a report. You choose the commits (i.e. a tag, a sha, or anything else you would reference in git that way). Then we use the “greater than” ( >) symbol to redirect output to a file (otherwise it would be placed in the console standard output). This is directed to our patch file. It really is that simple.

Also, if you actually want to apply a patch, you can use:

git diff -p1 < nameOfPatch.patch

Finally, one other cool tip: You can upload this patch file to your Bugzilla bug tracker and it will create a neatly formatted report for users viewing that bug.

Leave a Reply