Search code examples
gitbacktickscommand-substitutiongit-alias

Nest backticks inside a git command alias


I'm attempting to make a git alias to show all commits since the last tag. I'm basing it of this SO answer that I've used a lot in the past.

Currently, I'm trying this with a git config --global alias.* command like so:

git config --global alias.summary 'log `git describe --tags --abbrev=0`..HEAD --oneline'

This registers a new 'command' named 'summary' that would render out all the commit messages since the last tag.

However, when I run git summary, git throws out this error message:

fatal: ambiguous argument '`git': unknown revision or path not in the working tree.

Use '--' to separate paths from revisions, like this:

'git <command> [<revision>...] -- [<file>...]'

To me, this looks like the inner command git describe --tags --abbrev=0 that are nested inside the backticks do not evaluate correctly.

How can I fix this?


Solution

  • I've figured out that you need to use the bang/! operator to flag to git that the alias should execute in the shell.

    On a side note, replacing git describe --tags --abbrev=0 with $(git describe --tags --abbrev=0), causes the command to execute before it is placed into the [alias] section of the config file. This results in an alias command with the latest tag version being baked into the alias instead it dynamically finding the latest git tag.

    IE: If a repository had the tags 1.1.0 and 2.0.0 and I attempted to add the alias a few commits on from 2.0.0, it would generate the following alias command:

    '!git log 2.0.0..HEAD --oneline;'
    

    This means 2.0.0 is now fixed as the tag to check against rather than dynamically finding the latest tag for the repository in question.

    So, to fix my initial git alias, I needed to add !git to the start of the alias command:

    git config --global alias.summary '!git log `git describe --tags --abbrev=0`..HEAD --oneline;'