如何别名 git 命令您的 .bash_aliases 文件

How to alias git commands your .bash_aliases file

我正在尝试在我的 .bash_alias 文件中设置 git 别名。我知道您可以在 .gitconfig 文件中设置别名,但这会向别名添加 4 个额外的击键 ("git ")。我有几个在 .gitconfig 中工作的别名,但我无法让它们在 .bash_aliases.

中工作
alias glg='log --pretty=format:"%C(yellow)%h\ %ad%Cred%d\ %Creset%s%Cblue\ [%cn]" --decorate --date=short'
alias gll='log --pretty=format:"%C(yellow)%h%Cred%d\ %Creset%s%Cblue\ [%cn]" --decorate --numstat'
alias gld='log --pretty=format:"%C(yellow)%h\ %C(green)%ad%Cred%d\ %Creset%s%Cblue\ [%cn]" --decorate --date=short --graph'
alias gls='log --pretty=format:"%C(green)%h\ %C(yellow)[%ad]%Cred%d\ %Creset%s%Cblue\ [%cn]" --decorate --date=relative'
alias gb="!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"

任何人都可以告诉我我必须做什么才能使它正常工作吗?

您缺少前导 "git"!另外,不确定为什么有反斜杠。尝试:

alias glg='git log --pretty=format:"%C(yellow)%h %ad%Cred%d %Creset%s%Cblue [%cn]" --decorate --date=short'
alias gll='git log --pretty=format:"%C(yellow)%h%Cred%d %Creset%s%Cblue [%cn]" --decorate --numstat'
alias gld='git log --pretty=format:"%C(yellow)%h %C(green)%ad%Cred%d %Creset%s%Cblue [%cn]" --decorate --date=short --graph'
alias gls='git log --pretty=format:"%C(green)%h %C(yellow)[%ad]%Cred%d %Creset%s%Cblue [%cn]" --decorate --date=relative'
alias gb="git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"

但实际上,没有必要使用别名,这些最好写成函数。即:

glg() { git log --pretty=format:"%C(yellow)%h %ad%Cred%d %Creset%s%Cblue [%cn]" --decorate --date=short; }
gll() { git log --pretty=format:"%C(yellow)%h%Cred%d %Creset%s%Cblue [%cn]" --decorate --numstat; }
gld() { git log --pretty=format:"%C(yellow)%h %C(green)%ad%Cred%d %Creset%s%Cblue [%cn]" --decorate --date=short --graph; }
gls() { git log --pretty=format:"%C(green)%h %C(yellow)[%ad]%Cred%d %Creset%s%Cblue [%cn]" --decorate --date=relative; }
gb() { git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'; }