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:
git diff > changes.patch
And to put those changes back into your unstaged area, use the patch command:
patch -p0 < changes.patch
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
git format-patch master --stdout > changes.patch
See what changes a patch will make:
git apply --stat changes.patch
Check a patch:
git apply --check changes.patch
Apply and sign off a patch:
git am --signoff < changes.patch