GIT命令自定义别名逻辑的实现

169 阅读1分钟
title: GIT命令自定义别名逻辑的实现
date: 2018-01-05 00:00:00
updated: 2018-01-05 00:00:00
tags: [git,编程工具]
type: [git,编程工具]
comments:
description: 页面描述
keywords: 关键字
top_img:  页面顶部的图片
mathjax:
katex:
aside:
aplayer:
highlight_shrink: 配置代碼框是否展開(true/false)(默認為設置中highlight_shrink的配置)

概述

概述

别名用来帮助你定义自己的git命令。

比如你可以定义 git a 来运行 git add --all。

要添加一个别名, 一种方法是打开 ~/.gitconfig 文件并添加如下内容:

Windows平台下修改

 [alias]
   ch = checkout
   cm = commit
   p = push
   # Show verbose output about tags, branches or remotes
   tags = tag -l
   brs = branch -a
   remotes = remote -v
   

...或者在命令行里键入:

 $ git config --global alias.new_alias git_function

例如:

 $ git config --global alias.cm commit

指向多个命令的别名可以用引号来定义:

 $ git config --global alias.ac 'add -A . && commit'

下面,是我设置的一些自定义的命令:

 [alias]
     # git checkout 相关的自定义别名
     cha = checkout ./
   ch  = checkout 
     # git commit 相关的自定义别名
     c = commit
     cm = commit -m
     ca = commit --amend
     st = status
     # Show verbose output about tags, branches or remotes
     tags = tag -l
 ​
     # git branch 相关的自定义别名
     b = branch
     ba = branch -a
     # git pull 相关的自定义别名
     p  =  pull
     pr =  pull --rebase
     pm =  pull --merge
 ​
     # git push 相关的自定义别名
     po = push origin
     pfordev       = push origin HEAD:refs/for/dev
     pformaster    = push origin HEAD:refs/for/master
     pfor211       = push origin HEAD:refs/for/dev_2.1.1
     pmaster       = push origin master
     pdev          = push origin dev
     # git remote 相关的自定义别名
     rs = remote -v
 ​
     # git push 相关的自定义别名
     l =  log
 [color]
     ui = auto

下面是我的git config配置:

修改配置文件:

 gedit 
 [user]
   name = frewen
   email = frewen1225@gmail.com
 [core]
   quotepath = false
   excludesfile = /Users/wangzhijiang/.gitignore_global
   editor = /Applications/Sublime\ Text.app/Contents/MacOS/sublime_text
 [difftool "sourcetree"]
   cmd = opendiff "$LOCAL" "$REMOTE"
   path = 
 [mergetool "sourcetree"]
   cmd = /Applications/Sourcetree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
   trustExitCode = true
 [commit]
   template = /Users/wangzhijiang/.iov_git.template
 [alias]
     # git checkout 相关的自定义别名
     cha = checkout ./
     ch  = checkout 
     # git commit 相关的自定义别名
     c = commit
     cm = commit -m
     ca = commit --amend
     st = status
     # Show verbose output about tags, branches or remotes
     tags = tag -l
 ​
     # git branch 相关的自定义别名
     b = branch
     ba = branch -a
     # git pull 相关的自定义别名
     p  =  pull
     pr =  pull --rebase
     pm =  pull --merge
 ​
     # git push 相关的自定义别名
     po = push origin
     pfordev       = push origin HEAD:refs/for/dev
     pfor20        = push origin HEAD:refs/for/dev_2.0
     pfor211       = push origin HEAD:refs/for/dev_2.1.1
     pmaster       = push origin master
     pdev          = push origin dev
     # git remote 相关的自定义别名
     rs = remote -v
 ​
     # git push 相关的自定义别名
     l =  log
 [color]
     ui = auto
 ​
 ​