gitgit-config

Git alias script to treat all arguments as one string


Currently, I got following alias

[alias]
    comment = commit -m

It works fine for

git comment 'Export env'

But wanted to make it work without quotes

[alias]
    comment = !"git commit -m ${*}"

This runs into errors

$ git comment Export env
error: pathspec 'env' did not match any file(s) known to git
error: pathspec 'Export' did not match any file(s) known to git
error: pathspec 'env' did not match any file(s) known to git

Solution

  • Why do things in complex ways when the answer for question "How to write an alias that accepts parameters" is quite well-known — use a shell function inside an alias:

    git config alias.comment '!f() { git commit -m "$*"; }; f'
    

    Examples (with echo git commit "$*"):

    $ git comment test
    git commit -m test
    
    $ git comment foo bar
    git commit -m foo bar
    

    It's not obvious from the echo output it is exactly one parameter "foo bar" but it is.