You may be familiar with git stash
, but patches can also be very useful. Assuming you have changes to the working directory, instead of stashing it, we can create a patch:
git diff > example.patch
With that, we have created a patch file that contains the current diff. Now, instead of stashing, we can just discard all changes. To apply the patch, just do:
git apply example.patch
This is similar to running git stash pop
to update the current working directory with the diff from example.patch
. With patches, it’s easier to keep track of what each patch does. On top of that, you can distribute patches to other people so they can quickly apply the change to their repository. It’s super useful to create patches for testing a feature for example. Your reviewer can then just apply the patch when they’re reviewing your branch.
To revert the patch, simply do:
git apply -R example.path
You can use either -R
or --reverse
.
Tags: git