Lately, on independence day we needed to introduce a small logo change on our website to acknowledge it. The change wasn’t meant to last for more than a few days and hence, the problem of introducing a temporary change which shouldn’t be a part of the history arose. Out of ignorance, we went through the path of making a commit and later reverting it with
git-revert. Recently I found about excellent patching system of git which makes an impressive solution for the problem.
Patching used in git isn’t unique. It derives itself from an old
patch program written by Larry Wall in 1985.
patch program is pretty simple. It takes a diff file as its input and applies those changes to corresponding files. Because of this simplicity, it makes patch files very easy to send. Perhaps, before the advent of github, they were the old man’s pull request. Another great thing about patches is that they can painlessly be reversed.
format-patch is an extension of
patch and offers two advantages over it. First, it generates an email formatted patch file which is much easier to be sent via
mailutils. Secondly, it supports binary diffs which means if you have a binary file which was created in the patch, git’s patch file will have record of it and it will be created when patch is applied. The best thing about git’s patching system is that it can be applied even if the folder is not a git repository, making it suitable for use over production servers.
Temporary changes using format-patch
Depending upon your requirements, you can have multiple patch files for each commit, or a single one for multiple commits. For creating a temporary change, first create a new branch, say
abc and commit your local changes to it. After you have finished, you can use
git format-patch S-COMMIT --stdout > shiny.patch to create a single patch for multiple commits after
S-COMMIT. After saving the patch, you can delete the branch.
The saved patch can now be applied directly on production server with
git apply shiny.patch. To reverse this patch, you can use
git apply -R shin.patch.