gitgit-bashgit-for-windowsgit-alias

Git won't recognize aliases from local config file


Issue:

I have manually added a simple alias in ~/.gitconfig (git version 2.38.1.windows.1), and it looks like this:

[alias]
    lg = !git log --oneline --graph -$1 #

I use it as follows (expecting to get the equivalent result of typing git log --one-line --graph -5):

$ git lg 5

But it does not work, and I do not understand what am I missing as this same command works for me in other computer, the following error raises:

$ expansion of alias 'lg' failed; 'git' is not a git command

Double check:

The value of $HOME in my git bash is /c/Users/myself (as expected), therefore, if I execute the command git config --global -e it opens the file I modified with my alias. Also, after executing the command git config --list --show-origin I can see my alias as well:

file:C:/Users/myself/.gitconfig  alias.lg=!git log --oneline --graph -$1

Failed attempts (from other answers):

I have also tried adding the alias as a bash function:

[alias]
    lg = "!f() { git log --oneline --graph -$1; }; f"

But the issue remains, same if I remove the alias from the config file and try to add it from console:

$ git config alias.lg 'git log --oneline --graph -$1'

Also, there is no effect when removing symbols like ! or #.

This might be a dumb question but I do not know what else to try, I am kind of stuck here...


Solution

  • Aliases are just replacement token lists, there's no string-construction language going on there. When you need it you can use the shell's string-construction.

    You don't really need it here:

    [alias]
            lg = log --graph --oneline
    

    and then you can git lg -5 (and also add other arguments), but if it's worth the effort to construct an alternate command line rather than substitute tokens for the alias the way to do it is define a shell function that does the work and execute it with the alias args:

    [alias]
            lg = "!f() { git log --graph --oneline -$1; }; f"
    

    and then you can just git lg 5.

    By the way, here's my go-to log alias frontender:

            lgdo = "!f(){ git log --graph --decorate --oneline \"${@---all}\"; }; f"