1. 需求
Git 并不会在输入部分命令时自动推断出想要的命令
如果不想每次都输入完整的 Git 命令
可以通过 git config
文件来轻松地为每一个命令设置一个别名
2. 举例
这里有一些例子可以试试:
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
这意味着,当要输入 git commit
时,只需要输入 git ci
例如,为了解决取消暂存文件的易用性问题
可以向 Git 中添加自己的取消暂存别名:
$ git config --global alias.unstage 'reset HEAD --'
这会使下面的两个命令等价:
$ git unstage fileA
$ git reset HEAD -- fileA
通常也会添加一个 last
命令,像这样:
$ git config --global alias.last 'log -1 HEAD'
这样,可以轻松地 看到最后一次提交:
$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel <[email protected]>
Date: Tue Aug 26 19:48:51 2008 +0800
test for current head
Signed-off-by: Scott Chacon <[email protected]>
3. 外部命令
如果自己要写一些与 Git 仓库协作的工具的话
可能需要执行外部命令,而不是一个 Git 子命令
如果是那样的话,可以在命令前面加入 !
符号
如,将 git visual
定义为 gitk
的别名:
$ git config --global alias.visual '!gitk'
参考: git
以上内容,均根据git官网介绍删减、添加和修改组成
相关推荐:
Git笔记(9) 打标签
Git笔记(8) 远程仓库的使用
Git笔记(7) 撤消操作
Git笔记(6) 查看提交历史
Git笔记(5) 状态记录
谢谢