Last Updated: February 25, 2016
·
1.835K
· dsci

Git: Apply patches to code that is moved

Sometimes we face the situation to fix bugs in older versions of our apps. This fixes should also applied to other versions, but some code is moved to other directories or may be extracted in to a single library.

Again, with Git that isn't to hard. The combination of Git and a text editor works fine for me, but perhaps there is another solution. If you have one, feel free to share it in the comments section.

Okay. Let's say we fixed a bug in foo.coffee and this should be also included in another version. Creating a patch is the first step:

git diff HEAD~1 -- app/assets/javascripts/foo.coffee > fix_sorting_foo.patch

Fire up your text editor and open the patch. I'm in love with Sublime Text, it's very easy to do some replacing stuff we have to do in step 2:

diff --git a/app/assets/javascripts/foo.coffee b/app/assets/javascripts/foo.coffee
index 99a49a7..647456d 100644
--- a/app/assets/javascripts/foo.coffee 
+++ b/app/assets/javascripts/foo.coffee

The patch has to apply to vendor/assets/javascripts/foo.coffee, so replace all occurrences of app with vendor.

(For Sublime: place the cursor in front of app and press cmd+g, then type vendor).

Last step is to apply the patch:

git apply --stat fix_sorting_foo.patch # check what will be changed
git apply --check fix_sorting_foo.patch # check if it will apply
git apply fix_sorting_foo.patch

Done. Happy patching.