Bash – Errors with git alias shell command

bashgitshell-script

I'm using bash version 4.1.2(1)-release (x86_64-redhat-linux-gnu) on cygwin with git 1.7.1. I wanted to make an alias for a command that needed to use the input argument twice. Following these instructions, I wrote

[alias]
branch-excise = !sh -c 'git branch -D $1; git push origin --delete $1' --

and I get this error:

$> git branch-excise my-branch
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file

I've tried both a - and a -- at the end, but I get the same errors. How can I fix this?

Best Answer

man git-config says:

The syntax is fairly flexible and permissive; whitespaces are mostly ignored. The # and ; characters begin comments to the end of line, blank lines are ignored.

So:

branch-excise = !bash -c 'git branch -D $1; git push origin --delete $1'

is an equivalent to:

#!/usr/bin/env bash

bash -c 'git branch -D $1

Running the above script prints:

/tmp/quote.sh: line 3: unexpected EOF while looking for matching `''
/tmp/quote.sh: line 4: syntax error: unexpected end of file

One solution is to put a whole command in ":

branch-excise = !"bash -c 'git branch -D $1; git push origin --delete $1'"

However, it still doesn't work because $1 is empty:

$ git branch-excise master
fatal: branch name required
fatal: --delete doesn't make sense without any refs

In order to make it work you need to create a dummy function in .gitconfig and call it like this:

branch-excise = ! "ddd () { git branch -D $1; git push origin --delete $1; }; ddd"

Usage:

$ git branch-excise  master
error: Cannot delete the branch 'master' which you are currently on.
(...)
Related Question