Bash 和 Zsh 中配置命令别名提升效率

使用命令行操作 GitDocker 等时,命令较长,容易输错,影响效率,推荐配置别名(缩写),以提升效率。以下是在 ZshBash 中配置别名教程

配置 Git 别名

git config --global alias.st status  # git st
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.unstage 'reset HEAD'
git config --global alias.last 'log -1'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

系统配置全局别名

1、新建一个 aliases 文件

> vi ~/.zsh_aliases   # Zsh
> vi ~/.bash_aliases # Bash

aliases 文件内容:

# Git
alias gaa='git add .'
alias gau='git add --update'
alias gst='git status'
alias gl='git pull'
alias glo='git pull origin'
alias glom='git pull origin master'
alias glod='git pull origin dev'
alias glot='git pull origin test'
alias gp='git push'
alias gpfo='git push -f origin'
alias gpo='git push origin'
alias gpom='git push origin master'
alias gfom='git fetch origin master'
alias grom='git rebase origin/master'
alias gd='git diff | mate'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gcm='git commit -m'
alias gb='git branch'
alias gba='git branch -a'
alias gbd='git branch -D'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcot='git checkout -t'
alias gcom='git checkout master'
alias gcotb='git checkout --track -b'
alias gcod='git checkout dev'
alias gcot='git checkout test'
alias gcofw='git checkout feature/wxq'
alias glog='git log'
alias glogp='git log --pretty=format:"%h %s" --graph'
alias glg='git lg'
alias gsl='git stash list'
alias gss='git stash save'
alias gsp='git stash pop'
alias gm='git merge'
alias gma='git merge --abort'
alias grao='git remote add origin'
alias gr='git reset'
alias grs='git reset --soft'
alias grsh='git reset --soft HEAD^'

# Docker
alias dis="docker images"
alias di="docker image"
alias dps="docker ps"
alias dex="docker exec -it"
alias dlogs="docker logs --tail=20"
# K8s
alias kcu='kubectl config use-context'
alias kcc='kubectl config current-context'
alias k8t='kubectl config use-context [email protected]'
alias k8on='kubectl config use-context [email protected]'

2、添加 .xx_aliases 到 .xxrc 文件中

Zsh

> vi ~/.zshrc    # Zsh

...
source ~/.zsh_aliases # 底部新增

Bash

> vi ~/.bashrc   # Bash

...
source ~/.bash_aliases # 底部新增

3、刷新 .xxrc 配置

> source ~/.zshrc    # Zsh
> source ~/.bashrc # Bash

效果

通过 xx_aliases + source,就好像直接在 .xxrc 中设置的别名

Depp Wang wechat
个人公众号