配置 git alias 别名 和 bash alias

243 阅读1分钟

平台:windows

想要熟练的使用git,那 git alias 的使用是必不可少而。

如果你只打了某个指令的一部分,git 不会自动推测出你想要的命令。如果你懒得输入完整的 git 指令,这时候就需要了解 git config 来替指令设置别名。

例如:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

此时,你就可以只打 git co 而不需要打 git checkout

通过命令,单独配置每一个别名当然可以,你也可以直接编辑 .gitconfig 文件直接一次性输入所有你想要的别名。添加如下字段:

[user]
    name = xiangbei
    email = xiangbei@example.com
[alias]
    co = checkout
    br = branch
    cb = checkout -b
    cim = commit -m
    cm = checkout master
    lgo = log --oneline
    ps = push
    pl = pull
    st = status

你以为这就结束了吗?来看点更干的~

~/.bashrc 文件中:

alias gc='git checkout'
alias gb='git branch'
alias gci='git commit'
alias gs='git status'
alias p='pnpm'

之后,在gitbash中直接执行 p看看?

恭喜你,发现了!

相比于在 .gitcofig 文件配置 git alias 后, 执行命令需要搭配 git [your alias] 来触发 git 内部支持的参数别名,在 .bash_profile 中配置别名,支持任何在终端可以执行的有效命令。

加餐

.bashrc 和 .bash_profile 的区别和使用