1

I am trying to add alias for a default commit message, something like this:

alias gc='git commit -m "Default commit: $(date)"'

but I don't like date's default format and would like to change it to this:

date +'%A, %d %B %Y %H:%M:%S' # Tuesday, 02 May 2017 23:12:07

I run into problem of how to build this in alias. I cannot handle the multiple double and single quotes. Can someone help?


Edit.

Thanks for the suggestion on using function and the code. Based on that I have made this, slightly changed:

gc () 
{ 
    if [ "$#" == "0" ]; then
        itsmsg="Deafult commit";
    else
        itsmsg="$*";
    fi;
    git commit -m "$itsmsg ($(date +'%A, %d %B %Y %H:%M:%S'))"
}
Celdor
  • 2,437
  • 2
  • 23
  • 44

2 Answers2

4

As @123 mentioned, you should use a function instead of an alias. This eliminates a level of quoting.

gc () {
  git commit -m "Default commit: $(date +'%A, %d %B %Y %H:%M:%S')" "$@"
}
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks. It is indeed one of other solutions. But i don't know where to define functions and how to use them in shell. Besides, the gc purpose is to quickly commit. If i needed custom message, i would type full command. I'll fiddle with function and see how it goes. – Celdor May 02 '17 at 23:19
  • 1
    You define the function in the same place you currently define your alias, and you use it just like any other command. – chepner May 03 '17 at 00:44
  • To be clear, we're talking about shell aliases here, not git aliases as would be defined in the `[alias]` section of your `~/.gitconfig` file. – ghoti May 04 '17 at 14:16
  • What should this look like in a .gitconfig. This won't work to just add to the config. Thanks in advance! – Urasquirrel Apr 10 '20 at 16:25
2

Use ANSI C quoting so that you can escape single quotes inside single quotes:

alias gc=$'git commit -m "Default commit: $(date +\'%A, %d %B %Y %H:%M:%S\')"'
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • With three forms of quotes in use, you'd be in trouble if you wanted one more level of nesting. How do you nest single quotes inside single quotes? `echo 'You can'\''t.'` – ghoti May 04 '17 at 14:22