Git 使用教程(九):Git使用进阶
一、设置别名(alias)
我们知道我们执行的一些 Git 命令其实操作很频繁的类似有:
git commit
git checkout
git branch
git status
...
这些操作非常频繁,每次都要输入完全是不是有点麻烦,有没有一种简单的缩写输入呢?比如我想直接输入以下命令代替:
git c
git co
git br
git s
...
是不是很简单快捷啊?这个时候就用到了 alias 了,翻译过来就是别名的意思,输入以下命令就可以直接满足以上的需求。
git config --global alias.co checkout # 别名
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
当然以上别名不是固定的,你完全可以根据自己的习惯去定制,除此之外还可以设置组合,比如:
git config --global alias.psm 'push origin master'
git config --global alias.plm 'pull origin master'
之后经常用到的 git push origin master 和 git pull origin master 直接就用 git psm 和 git plm 代替了,是不是很方便?
另外这里给大家推荐一个很强大的 alias 命令,我们知道我们输入 git log 查看日志的时候是类似这样的:
$ git log
commit 3f063d841a75f8eb5d002e30bde182b8be76e9fa (HEAD -> master, tag: V1.0, origin/master, origin/HEAD)
Merge: c0d98d6 a8b5c5e
Author: liumingjun <405702878@qq.com>
Date: Fri Mar 23 16:44:46 2018 +0800
merged bug fix 101
commit a8b5c5e2cca21f5ffbbbec89cf22bdf90a29fd56
Author: liumingjun <405702878@qq.com>
Date: Fri Mar 23 16:42:34 2018 +0800
fix bug 101
commit c0d98d6499235d97fbdd6661de883eb53bc5e9c6
Merge: 24f92f4 1f9f1d5
Author: liumingjun <405702878@qq.com>
Date: Fri Mar 23 16:16:14 2018 +0800
conflict fixed
commit 24f92f49eb1b27eba2e38e667983e63830bc6041
Author: liumingjun <405702878@qq.com>
Date: Fri Mar 23 16:06:04 2018 +0800
coship
:...skipping...
commit 3f063d841a75f8eb5d002e30bde182b8be76e9fa (HEAD -> master, tag: V1.0, origin/master, origin/HEAD)
Merge: c0d98d6 a8b5c5e
Author: liumingjun <405702878@qq.com>
Date: Fri Mar 23 16:44:46 2018 +0800
merged bug fix 101
告诉大家一个比较屌的命令,输入
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
* 3f063d8 - (HEAD -> master, tag: V1.0, origin/master, origin/HEAD) merged bug fix 101 (80 minutes ago) <liumingjun>
|\
| * a8b5c5e - fix bug 101 (83 minutes ago) <liumingjun>
|/
* c0d98d6 - conflict fixed (2 hours ago) <liumingjun>
|\
| * 1f9f1d5 - money (2 hours ago) <liumingjun>
* | 24f92f4 - coship (2 hours ago) <liumingjun>
|/
* f8c2cfa - branch test (2 hours ago) <liumingjun>
* c6bdeb5 - remove test.txt (7 hours ago) <liumingjun>
* c61ac4f - add test.txt (7 hours ago) <liumingjun>
* 35b7969 - append GPL (21 hours ago) <liumingjun>
* 136d10c - add two lines (21 hours ago) <liumingjun>
* 1186b5e - creat a new file readme (22 hours ago) <liumingjun>
是不是比较清晰,整个分支的走向也很明确,但是每次都要输这么一大串是不是也很烦?这时候你就该想到 alias 啊:
$ git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
这样以后直接输入 git lg 就行了。