简体   繁体   English

如何让别名在 .gitconfig 中工作?

[英]How to get aliases working in .gitconfig?

Github has the following recommendation for global git configuration ~/.gitconfig : Github对全局 git 配置有以下建议~/.gitconfig

[alias]             # Is this [-] only a comment in .gitconfig?
gb = git branch
gba = git branch -a
gc = git commit -v
gd = git diff | mate  
gl = git pull
gp = git push
gst = git status

The above commands worked in my old Git.上面的命令在我的旧 Git 中工作。 However, they do not work now for some unknown reason.但是,由于某些未知原因,它们现在不起作用。

The problem seems not to be in the commands.问题似乎不在命令中。 It is perhaps in another git related file which controls which file affects aliases.它可能在另一个与 git 相关的文件中,它控制哪个文件影响别名。

How can you get the aliases to work?如何让别名起作用?

The first thing to be aware of is that the git aliases only apply when you are calling git, so an alias of st = status will take effect when you run:首先要注意的是 git 别名仅在您调用 git 时适用,因此st = status的别名将在您运行时生效:

$ git st

If you want to be able to do:如果你想能够做到:

$ gst

To run git status you would need to set up an alias for bash (or whatever shell you use).要运行git status ,您需要为 bash(或您使用的任何 shell)设置一个别名。

Well, for aliases which are simply shorter versions of git commands (like st for status ), you do not need to add the git prefix to it.好吧,对于只是 git 命令的较短版本的别名(例如st代表status ),您不需要为其添加git前缀。 Additionally, if you want to execute a shell command rather than a git sub-command, you must prefix the alias definition with an exclamation point, as specified in git-config(1) .此外,如果要执行 shell 命令而不是 git 子命令,则必须在别名定义前加上感叹号,如git-config(1)中所指定。 My alias section of my ~/.gitconfig looks like this:我的~/.gitconfig的别名部分如下所示:

[alias]
    st = status
    ci = commit -s
    br = branch
    co = checkout
    vis = !gitk --all &

And then I can run:然后我可以运行:

$ git st # Runs "git status"
$ git ci # Runs "git commit -s"
$ git vis # runs "gitk --all &"

And so on.等等。

I believe what GitHub is referring to is system aliases , not '.gitconfig' aliases.我相信 GitHub 指的是系统别名,而不是 '.gitconfig' 别名。

In other terms, you would need to type, like illustrated here , the following Unix command to make those 'aliases' work:换句话说,您需要输入如下所示Unix命令,以使这些“别名”工作:

alias g=’git’
alias gb=’git branch’
alias gba=’git branch -a’
alias gc=’git commit -v’
alias gca=’git commit -v -a’
alias gd=’git diff | mate’
alias gl=’git pull’
alias gp=’git push’

在我的例子中,我导航到 .zsh 文件夹并将这个 repo 克隆为git clone https://github.com/mdumitru/git-aliases.git ,然后source ~/.zshrc并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM