I have a bunch of git aliases already set up in my .bash_profile that work correctly:

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"

I'm trying to add an alias for the following command, but keep running into an error:

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

What I'd like to do, is be able to type:

glog 'some amount of time'

So, being new at both aliases and git, I figured this would work:

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

It throws the following error:

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>...]'

How can I correct my alias to make this work?

Thanks!

[EDIT]

I can make it work if I change the alias to:

alias glog="git log --all --pretty=format:'%h %cd %s (%an)'"

and then just type in:

glog --since='some amount of time'

but I'd really like to just type the amount of time, if possible.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top