Last Updated: February 25, 2016
· 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 and this should be also included in another version. Creating a patch is the first step:

git diff HEAD~1 -- app/assets/javascripts/ > 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/ b/app/assets/javascripts/
index 99a49a7..647456d 100644
--- a/app/assets/javascripts/ 
+++ b/app/assets/javascripts/

The patch has to apply to vendor/assets/javascripts/, 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.