09.设置别名

顾名思义,别名就是另外一个名字,大名不好叫,取一个狗蛋的名字,好记又好叫。Git 支持命令别名,你可以通过修改配置文件或者使用命令的方式告诉 Git 你要使用别名的命令。

下面的命令是上一节的格式化提交的命令,我们现在可以将命令缩短为 git hist。就可以得到我们想要的效果。

git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

通过配置文件设定别名

首先,打开终端,在终端中输入 git config --show-origin 查看配置文件目录,我们修改用户配置文件 gitconfig。在配置文件中添加 alias ,将别名和替代的命令写入其中并保存。

[user]
    name = aku
    email = aku@example.com
[core]
    editor = code --wait
[alias]
    hist = "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
    ci = commit
    st = status

然后你就可以使用 git hist 来格式化输出了。

$ git hist
* d7f681f 2023-05-05 | Added abc to the test.txt (HEAD -> master) [aku]
* 01b8702 2023-05-05 | Add first file [aku]

同理,我们也可以将 commit 设置成 ci

通过命令设定

另外一种方式是通过 git config命令来实现。比如我们要将 status 命令设置为 st,可以使用如下命令。

git config --global alias.st 'status'

Last updated