bash 中的别名以将格式化的当前日期添加到 git 提交消息

Alias in bash to add formatted current date to git commit message

我正在尝试为默认提交消息添加别名,如下所示:

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

但我不喜欢日期的默认格式,想将其更改为:

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

我 运行 遇到了如何在别名中构建它的问题。我无法处理多个双引号和单引号。有人可以帮忙吗?


编辑。

感谢您对使用函数和代码的建议。基于此,我做了这个,稍作改动:

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

使用 ANSI C 引号,以便您可以在单引号内转义单引号:

alias gc=$'git commit -m "Default commit: $(date +\'%A, %d %B %Y %H:%M:%S\')"'

如@123 所述,您应该使用函数而不是别名。这消除了引用级别。

gc () {
  git commit -m "Default commit: $(date +'%A, %d %B %Y %H:%M:%S')" "$@"
}