Creating a 'git log' alias with formatting

血红的双手。 提交于 2019-12-06 08:16:55

Instead, you can create a function in .bash_profile. It will allow you to use variables:

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

And call it like usual:

glog "7 days ago"

quick follow-up: how would I change the function to allow the possibility of also appending the --author="so-and-so" flag? as in, I could type glog "7 days ago" or blog "7 days ago" --author="bob"

I would do as like follows:

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
}

So you can call it with

glog "7 days ago"
glog "7 days ago" "bob"

Note that the if [ -z "$2" ]; then condition is checking if the second parameter is empty. If so, just executes the code without author. Otherwise, it uses it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!