Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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 $*
Owner

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