Search code examples
gitmsysgitgit-bashbackticksgit-alias

Using backtick command execution in Git alias


Here's a simple example to demonstrate my problem. If I do:

git commit -m "`date --utc`"

It executes date --utc, and puts the result inside the commit message.

However, when I alias it to testcomit:

git config --global alias.testcommit 'commit -m "`date --utc`"'

Doing git testcommit does not execute the `date --utc` part, it instead puts it verbatim in the commit message.

So, how do I get this alias to execute date --utc?


Solution

  • From the documentation:

    If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command.

    So you can use:

    git config --global alias.testcommit '!git commit -m "$(date --utc)"'
    

    Note that you have to put git in there, since you're now specifying the whole shell command.

    ($() instead of backticks is unrelated, but it's better so you should start using it.)