Search code examples
gitcmdgit-loggit-alias

Unable to register git `log` alias using CMD


I am trying to alias a log command using Windows CMD (using " because of Windows, see this answer):

git log --pretty=format:"%C(yellow)%h %C(red)%ad %C(brightcyan)%d %C(white)%s" --date=format:"%Y-%m-%d %H:%M"

The command works fine when I execute it on CMD, however when I try to save it as an alias this is not working:

> git config --system alias.time-log log --pretty=format:"%C(yellow)%h %C(red)%ad %C(brightcyan)%d %C(white)%s" --date=format:"%Y-%m-%d %H:%M"
usage: git config [<options>]
<bla bla bla...>

So basically there is some error going on, it does not recognize the command. I have tried aliasing simpler commands:

> git config --system alias.time-log log --pretty=format:"%C(yellow)%h"
> git config --system alias.time-log log --pretty=format:'%C(yellow)%h'

but contrary to the solution given in the answer I post above, the section after log now gets truncated. See when I query my list of aliases:

> config --get-regexp ^alias\.
alias.time-log log
alias.time-log log

Background

A few days back I managed to alias the following command without problem (I think I did use double quotes "):

git log --pretty=format:"%C(yellow)%h %C(red)%ad %C(brightcyan)%d %C(white)%s" --date=short

This is how it shows when I query my saved aliases:

> config --get-regexp ^alias\.
alias.date-log log --pretty=format:'%C(yellow)%h %C(red)%ad %C(brightcyan)%d %C(white)%s' --date=short

Edit

I have tried using Git Bash but with no success either:

$ git config --system alias.time-log log --pretty=format:'%C(yellow)%h %C(red)%ad %C(brightcyan)%d %C(white)%s' --date=format:'%Y-%m-%d %H:%M'
usage: git config [<options>]
<bla bla bla...>

Solution

  • Since your command is composed of several options separated by space and some of them already use double quotes, you do need to enclose the alias within single quotes.

    git config --system alias.time-log 'log --pretty=format:"%C(yellow)%h %C(red)%ad %C(brightcyan)%d %C(white)%s" --date=format:"%Y-%m-%d %H:%M"'
    

    In general, when supplying a command as a parameter, it's always a good practice to enclose it within single or double quotes.