使用格式创建'git log'别名 [英] Creating a 'git log' alias with formatting

查看:171
本文介绍了使用格式创建'git log'别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 别名gst =git状态
别名gl =git pull
别名gp =git push
别名gd =git diff | mate
别名gc =git commit -v
别名gca =git commit -v -a
别名gb =git branch
别名gba =git branch -a



我试图给以下命令添加一个别名,但仍然遇到错误:

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

我想要做的是能够输入:

  glog'一段时间'

新的在别名和混帐,我想这会工作:

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

出现以下错误:

  fat al:7天前的模棱两可的论点:未知的版本或路径不在工作树中。 
使用' - '分隔修订版本的路径,如下所示:
'git< command> [< revision> ...] - [< file>]'

如何纠正我的别名,使其工作?



谢谢!

/ strong>



如果我将别名更改为:

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

,然后输入:

  glog --since ='一定数量的时间'

但我真的很想只输入时间量,如果可能的话。 .bash_profile 中创建一个函数。它允许你使用变量:

$ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ -pretty = format:'%h%cd%s(%an)'--since =$ 1
}

并像往常一样称呼:

  glog7天前

code>







快速追踪:我将如何变更允许$​​ b $ b可能性的函数还附加了--author =so-and-so标志?例如,I
可以输入glog7天前或博客7天前--author =bob




  glog()
{
if [-z $ 2];然后
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
}
$ b

  

glog7天前
glog7天前bob

请注意, if [-z$ 2];那么条件是检查第二个参数是否为空。如果是这样,只需执行没有 author 的代码。否则,它会使用它。


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.

这篇关于使用格式创建'git log'别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆