linuxgitgit-config

Is it possible to write git alias functions with single quotes?


I'm trying to write a git alias function with single quotes to rename and commit files that may have spaces in their names, but it does not work :

$ grep '^\s*mvv' ~/.gitconfig 
    mvv = '!f() { cd ${GIT_PREFIX:-.};git mv -v "$1" "$2";git commit -uno "$1" "$2" -m "Renamed $1 to $2"; }; f'
$ git mvv "9.1.3 Packet Tracer - Identify MAC and IP Addresses - ILM.pdf" 9.1.3_Packet_Tracer_-_Identify_MAC_and_IP_Addresses_-_ILM.pdf
fatal: bad alias.mvv string: unclosed quote
$ git config alias.mvv
'!f() { cd ${GIT_PREFIX:-.}

I also tried this :

$ grep '^\s*mvv' ~/.gitconfig 
    mvv = !sh -c '{ cd ${GIT_PREFIX:-.};git mv -v "$1" "$2";git commit -uno "$1" "$2" -m "Renamed $1 to $2"; }'
$ git mvv "9.1.3 Packet Tracer - Identify MAC and IP Addresses - ILM.pdf" 9.1.3_Packet_Tracer_-_Identify_MAC_and_IP_Addresses_-_ILM.pdf
sh -c '{ cd ${GIT_PREFIX:-.}: 1: Syntax error: Unterminated quoted string
$ git config alias.mvv
!sh -c '{ cd ${GIT_PREFIX:-.}
$

But when I run it, is says fatal: bad alias.mvv string: unclosed quote error.

I expect it to work.


Solution

  • The short answer seems to be "no, use double quotes". To avoid escaping internal quotes manually one can ask Git to do escaping:

    git config alias.mvv '!f() { cd ${GIT_PREFIX:-.};git mv -v "$1" "$2";git commit -uno "$1" "$2" -m "Renamed $1 to $2"; }; f'
    

    then see .git/config — Git escapes double quotes in the alias:

    $ cat .git/config 
    [alias]
            mvv = "!f() { cd ${GIT_PREFIX:-.};git mv -v \"$1\" \"$2\";git commit -uno \"$1\" \"$2\" -m \"Renamed $1 to $2\"; }; f"
    

    PS. Only works in Unix/Linux or Linux-like environments (read git-bash); AFAIK Windows command interpreters don't like apostrophes (single quotes) in command line. Somebody with better knowledge please correct me if I'm wrong.