git 命令別名配置

作者:辰風沐陽 閱讀:2706 發(fā)布時間:2022-01-06 上次更新:2022-11-19

1. 前言


在使用 git 的過程中,有些命令使用頻率很高,并且命令可能還很長,敲起來不僅效率慢,還容易寫錯

為了便捷輸入,git 提供了給命令設(shè)置別名的功能,我們可以定義命令的別名,通過簡單的別名快速使用復(fù)雜且長的命令

本文示例中用的都是全局級別--global 配置,倉庫級別--local、系統(tǒng)級別 --system 亦如此

2. 設(shè)置別名


語法格式

  1. git config [命令級別] alias.別名 '命令'

下面是我常用的別名設(shè)置示例:

  1. git config --global alias.s status
  2. git config --global alias.a 'add .'
  3. git config --global alias.cm 'commit -m'

使用別名

  1. git s
  2. git a
  3. git cm 'first commit'

查看定義的別名

  1. git config --global -l | grep alias

3. 取消別名


語法格式

  1. git config --global --unset alias.別名

使用示例

  1. git config --global --unset alias.s

當然,也可以給 取消別名命令 設(shè)置別名,比如: 別名定義為 u

  1. git config --global alias.u 'config --global --unset'

然后就可以通過別名 u 來取消別名設(shè)置了

  1. git u alias.s

補充: 也可以打開別名配置文件,直接在配置文件中增加、刪除或修改別名 (-e 參數(shù)會以 vi 命令模式打開配置文件)

  1. git config --global -e

4. 系統(tǒng)配置定義別名


一. Mac 系統(tǒng)

mac 用戶如果已經(jīng)安裝了 zsh,可在 ~/.zshrc 文件中添加以下內(nèi)容定義別名

  1. alias gi="git init"
  2. alias gs="git status"
  3. alias ga="git add -A"
  4. alias gc="git commit -m"
  5. 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)容如下

  1. # generated by Git for Windows
  2. test -f ~/.profile && . ~/.profile
  3. test -f ~/.bashrc && . ~/.bashrc

然后將別名指令放入 ~/.bash_profile 中,如下圖所示(重新打開 Git Bash Here 窗口生效):

有時可能忘記定義的別名命令,那么可以再增加一個別名定義。這樣就可以通過 gas 命令查看定義的別名

  1. # windows git bash
  2. alias gas="cat ~/.bash_profile | grep alias"

5. 我的 git 命令別名定義


在 Windows 系統(tǒng)中查看定義的別名:

  1. # windows
  2. alias gas="cat ~/.bash_profile | grep alias"

因為我在日常開發(fā)中會經(jīng)常使用 git,所以我選擇使用系統(tǒng)配置定義別名。下面是我經(jīng)常用的別名:

  1. # git command
  2. alias gi="git init"
  3. alias gs="git status"
  4. alias ga="git add -A"
  5. alias gc="git commit -m"
  6. alias guc="git commit -am"
  7. alias gca="git commit --amend -m"
  8. alias gac="git add -A && git commit -m"
  9. alias go="git checkout"
  10. alias gp="git push"
  11. alias gb="git branch -avv"
  12. alias gr="git remote -v"
  13. alias gbr="git branch -avv && git remote -v"
  14. alias geh="git push origin gitee && git push origin github"
  15. alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

標簽: git