使用格式创建"git log"别名

log*_*son 2 git bash terminal alias .bash-profile

我已经在我的.bash_profile中设置了一堆git别名,它们可以正常工作:

alias gst="git status"
alias gl="git pull"
alias gp="git push"
alias gd="git diff | mate"
alias gc="git commit -v"
alias gca="git commit -v -a"
alias gb="git branch"
alias gba="git branch -a"
Run Code Online (Sandbox Code Playgroud)

我正在尝试为以下命令添加别名,但仍然遇到错误:

git log --all --pretty=format:'%h %cd %s (%an)' --since='7 days ago'
Run Code Online (Sandbox Code Playgroud)

我想做的是,能够键入:

glog 'some amount of time'
Run Code Online (Sandbox Code Playgroud)

所以,在别名和git中都是新手,我认为这样可行:

alias glog="git log --all --pretty=format:'%h %cd %s (%an)' --since="
Run Code Online (Sandbox Code Playgroud)

它会引发以下错误:

fatal: ambiguous argument '7 days ago': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Run Code Online (Sandbox Code Playgroud)

如何更正我的别名以使其工作?

谢谢!

[编辑]

如果我将别名更改为:

alias glog="git log --all --pretty=format:'%h %cd %s (%an)'"
Run Code Online (Sandbox Code Playgroud)

然后输入:

glog --since='some amount of time'
Run Code Online (Sandbox Code Playgroud)

但如果可能的话,我真的只想输入一定的时间.

fed*_*qui 8

相反,您可以在中创建一个函数.bash_profile.它将允许您使用变量:

glog ()
{
        git log --all --pretty=format:'%h %cd %s (%an)' --since="$1"
}
Run Code Online (Sandbox Code Playgroud)

并像往常一样调用它:

glog "7 days ago"
Run Code Online (Sandbox Code Playgroud)

快速跟进:如何更改函数以允许还附加--author ="so-and-so"标志的可能性?在,我可以输入glog"7天前"或博客"7天前"--author ="bob"

我会这样做如下:

glog ()
{
    if [ -z "$2" ]; then
       git log --all --pretty=format:'%h %cd %s (%an)' --since="$1"
    else
       git log --all --pretty=format:'%h %cd %s (%an)' --since="$1" --author="$2"
    fi
}
Run Code Online (Sandbox Code Playgroud)

所以你可以用它来调用它

glog "7 days ago"
glog "7 days ago" "bob"
Run Code Online (Sandbox Code Playgroud)

请注意,if [ -z "$2" ]; then条件是检查第二个参数是否为空.如果是这样,只需执行代码即可author.否则,它使用它.