git 别名以及使用参数相关注意事项
Aliases
git 别名配置在文件 ~/.gitconfig
中,存在两种别名
- git 命令别名,内置命令
- 非 git 命令别名,使用前缀
!
,运行在 shell
[alias]
ci = commit
br = branch
push-view = !sh -c 'git origin HEAD:refs/for/$1' -
gitkconflict = !gitk --left-right HEAD...MERGE_HEAD
Advanced aliases with parameters
Starting with version 1.5.3, git supports appending the arguments to commands prefixed with “!”, too. If you need to perform a reordering, or to use an argument twice, you can use this trick:
[alias]
example = !sh -c 'ls $2 $1' -
!
在 shell 中运行$1
为第一个参数
另外一种方式,封装为 bash
函数 f
:
[alias]
files = "!f() { git diff --name-status \"$1^\" \"$1\"; }; f"
Debug
调试输出信息
$ GIT_TRACE=1 git push-view test
或者使用 gdb
调试
[alias]
debug = !GIT_PAGER= gdb --args git
用法 git debug push-view test
Problem
定义 gerrit review alias 为
[alias]
...
push-for-review = push origin HEAD:refs/for/master
push-for-review-branch = !git push origin HEAD:refs/for/$1
push-as-draft = push origin HEAD:refs/drafts/master
push-as-draft-branch = !git push origin HEAD:refs/drafts/$1
...
调试如下
$ GIT_TRACE=1 git push-for-review-branch test
16:38:33.280667 git.c:344 trace: built-in: git push origin HEAD:refs/for/test test
To ssh://192.168.110.254:29418/misc
d9d506f..20c5d70 test -> test
* [new branch] HEAD -> refs/for/test
尾巴上多出一个参数导致直接合并,不理解为什么?$@
解析问题?
stackoverflow - Git alias with positional parameters解释如下
The sh -c ‘..’ – and f() {..}; f options both cleanly handle the “$@” parameters in different ways (see with GIT_TRACE). Appending “#” to an alias would also allow positional parameters without leaving the trailing ones.
修改为
[alias]
...
push-for-review = push origin HEAD:refs/for/master
push-for-review-branch = "!git push origin HEAD:refs/for/$1 #"
push-as-draft = push origin HEAD:refs/drafts/master
push-as-draft-branch = "!git push origin HEAD:refs/drafts/$1 #"
...