Skip to content

Instantly share code, notes, and snippets.

@kfish
Created November 12, 2013 03:59
Show Gist options
  • Save kfish/7425248 to your computer and use it in GitHub Desktop.
Save kfish/7425248 to your computer and use it in GitHub Desktop.
Apply a patch file that was produced with "git format-patch" using the patch command, and commit it using the message from the original commit.
#!/bin/bash
apply () {
filename=$1
shift
patch_args=$*
gotSubject=no
msg=""
cat $filename | while read line; do
if [ "$line" == "---" ]; then
patch $patch_args -p1 < $filename
git commit -a -m "$msg"
break
fi
if [ "$gotSubject" == "no" ]; then
hdr=(${line//:/ })
if [ "${hdr[0]}" == "Subject" ]; then
gotSubject=yes
msg="${hdr[@]:3}"
fi
else
msg="$msg $line"
fi
done
}
apply $*
@kfish
Copy link
Author

kfish commented Nov 12, 2013

Apply a patch file that was produced with "git format-patch" using the patch command, and commit it using the message from the original commit.

Unlike "git am", "patch" supports fuzzy matches. If "git am" fails, try running this command as:

$ ./apply-patch.sh 0037-Fix-broken-foo.patch -F3

to increase the fuzz factor to 3. Arguments after the patch filename are passed verbatim to patch.

Also, "patch" is not all-or-nothing; so if 1 of 7 hunks fail then this script will commit the 6 successful hunks, and you can inspect the rejected hunk in the foo.rej file then amend the commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment