1. 前言
在使用 git 的過程中,有些命令使用頻率很高,并且命令可能還很長,敲起來不僅效率慢,還容易寫錯
為了便捷輸入,git 提供了給命令設(shè)置別名的功能,我們可以定義命令的別名,通過簡單的別名快速使用復(fù)雜且長的命令
本文示例中用的都是全局級別--global
配置,倉庫級別--local
、系統(tǒng)級別 --system
亦如此
2. 設(shè)置別名
語法格式
git config [命令級別] alias.別名 '命令'
下面是我常用的別名設(shè)置示例:
git config --global alias.s status
git config --global alias.a 'add .'
git config --global alias.cm 'commit -m'
使用別名
git s
git a
git cm 'first commit'
查看定義的別名
git config --global -l | grep alias
3. 取消別名
語法格式
git config --global --unset alias.別名
使用示例
git config --global --unset alias.s
當然,也可以給 取消別名命令 設(shè)置別名,比如: 別名定義為 u
git config --global alias.u 'config --global --unset'
然后就可以通過別名 u 來取消別名設(shè)置了
git u alias.s
補充: 也可以打開別名配置文件,直接在配置文件中增加、刪除或修改別名 (-e 參數(shù)會以 vi 命令模式打開配置文件)
git config --global -e
4. 系統(tǒng)配置定義別名
一. Mac 系統(tǒng)
mac 用戶如果已經(jīng)安裝了 zsh,可在 ~/.zshrc
文件中添加以下內(nèi)容定義別名
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 系統(tǒng)
打開 Git Bash Here
,如果 ~/.bash_profile
文件不存在,就先創(chuàng)建,文件內(nèi)容如下
# 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 系統(tǒng)中查看定義的別名:
# windows
alias gas="cat ~/.bash_profile | grep alias"
因為我在日常開發(fā)中會經(jīng)常使用 git,所以我選擇使用系統(tǒng)配置定義別名。下面是我經(jīng)常用的別名:
# 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"