Git alias script to treat all arguments as one string

34 Views Asked by At

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
2

There are 2 best solutions below

0
phd On BEST ANSWER

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.

2
William Pursell On

You could invoke a shell. eg:

[alias]
        comment = !bash -c 'git commit -m \"$*\"' _