Create and apply a patch file with Git

If you’re no stranger to working on multiple projects throughout the day, you’re probably going to be working on multiple tasks within the same project as well. In a fast-paced environment (like your usual digital agency), you’ll probably find yourself in a position where you need to stash your current changes to complete another task which has a higher priority.
Sometimes you’re able to git stash your changes and use git stash apply to put them back when you’re ready however, sometimes it might be necessary for someone else to continue the work you started. If you’re not working on your own change/feature/hotfix branch (using git flow) then it might be easier to send over a patch file to the lucky developer tasked to finish your work.
In the same way git stash will stash your unstaged files, the contents of your git diff will essentially be your patch file.
To create a patch file from git diff simply run the following command:

And to put those changes back into your unstaged area, use the patch command:

This is a very quick and dirty way of creating and applying patches however, Git comes with some useful commands to dry run, check and apply patches for you. Using the native commands, you can do the following:

Create a patch

See what changes a patch will make:

Check a patch:

Apply and sign off a patch:

Leave a Reply

Your email address will not be published. Required fields are marked *