Export specific Git commits as patches

bash, git

Solution

My git was implicitly using the `--color` option so the `$COMMIT` string contained some color codes. So this messed up the parsing. Interesting, that also the color was contained in my error messages.

But you do not see this in the terminal (it's just colorful), and also not after pasting to stack overflow.

The solution is:

commits=`git log --no-color --pretty=oneline | grep "#1234" | cut -f1 -d" "`

Problem

I am trying to export several commits (those that contain a specific ticket number 1234 in the commit message) to patch files. EDIT The working script is on https://github.com/amenk/SelfScripts/blob/master/git-extract-patches This is what I have ``` #!/bin/bash -x commits=`git log --pretty=oneline | grep "#1234" | cut -f1 -d" "` no=1; for COMMIT in $commits do git format-patch -1 $COMMIT --start-number=$no no=$(($no+1)) done ``` But for some reason executing of git format-patch fails: ``` $ ./getpatches.sh ++ git log --pretty=oneline ++ grep '#6809' ++ cut -f1 '-d ' + commits='da591d66f05513488ee06857edc9d24a046c179d 4fd781da9cc503b961f8e4c42bbb136d9e3c1806 3a9311f5507f91f830b44673c57f672e7aabaac0' + no=1 + for COMMIT in '$commits' + git format-patch -1 'da591d66f05513488ee06857edc9d24a046c179d' --start-number=1 fatal: ambiguous argument 'da591d66f05513488ee06857edc9d24a046c179d': unknown revision or path not in the working tree. Use '--' to separate paths from revisions ``` When I call `git format-patch -1 'da591d66f05513488ee06857edc9d24a046c179d' --start-number=1` manually, everything is fine. EDIT: I think it is something with the quotes. If I add a `git log | grep $COMMIT` into the loop, I get the following error: ``` + grep '992ab41d3539539bd609209beed33a9de2f4277a' grep: Unmatched [ or [^ ``` Another interseting effect is, if I hard-code `grep '992ab41d3539539bd609209beed33a9de2f4277a'` in the for loop, the command output (because of the `-x` option for `bash` is without the quotes and it works. ``` + grep 992ab41d3539539bd609209beed33a9de2f4277a ``` Where are these quotes coming from and how do I get rid of them?

Original source