[TOC] #### 1. 前言 --- 在使用 git 的过程中,有些命令使用频率很高,并且命令可能还很长,敲起来不仅效率慢,还容易写错 为了便捷输入,git 提供了给命令设置别名的功能,我们可以定义命令的别名,通过简单的别名快速使用复杂且长的命令 本文示例中用的都是全局级别`--global` 配置,仓库级别`--local`、系统级别 `--system` 亦如此 #### 2. 设置别名 --- 语法格式 ``` git config [命令级别] alias.别名 '命令' ``` 下面是我常用的别名设置示例: ```bash git config --global alias.s status git config --global alias.a 'add .' git config --global alias.cm 'commit -m' ``` 使用别名 ```bash git s git a git cm 'first commit' ``` 查看定义的别名 ``` git config --global -l | grep alias ``` #### 3. 取消别名 --- 语法格式 ```bash git config --global --unset alias.别名 ``` 使用示例 ```bash git config --global --unset alias.s ``` 当然,也可以给 **取消别名命令** 设置别名,比如: 别名定义为 u ``` git config --global alias.u 'config --global --unset' ``` 然后就可以通过别名 u 来取消别名设置了 ```bash git u alias.s ``` 补充: 也可以打开别名配置文件,直接在配置文件中增加、删除或修改别名 (-e 参数会以 vi 命令模式打开配置文件) ``` git config --global -e ``` #### 4. 系统配置定义别名 --- **一. Mac 系统** mac 用户如果已经安装了 zsh,可在 `~/.zshrc` 文件中添加以下内容定义别名 ``` alias gi="git init" alias gs="git status" alias ga="git add -A" alias gc="git commit -m" alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" ```  --- **二. Windows 系统** 打开 `Git Bash Here`,如果 `~/.bash_profile` 文件不存在,就先创建,文件内容如下 ``` # generated by Git for Windows test -f ~/.profile && . ~/.profile test -f ~/.bashrc && . ~/.bashrc ``` 然后将别名指令放入 `~/.bash_profile` 中,如下图所示(重新打开 Git Bash Here 窗口生效):  有时可能忘记定义的别名命令,那么可以再增加一个别名定义。这样就可以通过 `gas` 命令查看定义的别名 ``` # windows git bash alias gas="cat ~/.bash_profile | grep alias" ``` #### 5. 我的 git 命令别名定义 --- 在 Windows 系统中查看定义的别名: ``` # windows alias gas="cat ~/.bash_profile | grep alias" ``` 因为我在日常开发中会经常使用 git,所以我选择使用系统配置定义别名。下面是我经常用的别名: ``` # git command alias gi="git init" alias gs="git status" alias ga="git add -A" alias gc="git commit -m" alias guc="git commit -am" alias gca="git commit --amend -m" alias gac="git add -A && git commit -m" alias go="git checkout" alias gp="git push" alias gb="git branch -avv" alias gr="git remote -v" alias gbr="git branch -avv && git remote -v" alias geh="git push origin gitee && git push origin github" alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" ```