自定义

配置

git 除了 可以配置user.nameuser.email, git 还有很多可以配置的选项

例如: 让 Git 显示颜色

git config --global color.ui true

更多的配置文件:这里open in new window

配置别名

你是否觉得 git 的指令太长了, 经常敲错指令

可以给命令配置别名,例如:git status 可以用 git st 代替

git config --global alias.st status

还有别的命令可以简写,很多人都用 co 表示 checkout,ci 表示 commit,br 表示 branch:

git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch

TIP

--global参数是全局参数,也就是这些命令在这台电脑的所有 Git 仓库下都有用。

如果你很熟悉 unix 系统,那么你就会知道 alias 可以实现更多,例如:

使用命令git reset HEAD file 可以把暂存区的修改撤销掉(unstage),那么我们可以配置成这样

git config --global alias.unstage 'reset HEAD'

就可以使用 git unstage file 来撤销修改辣

你也可以配置:

git config --global alias.last 'log -1'

就可以使用 git last 看到最后一次提交信息

甚至:

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

使用 git lg 就可以看到多彩多样的 log 了

配置文件

配置 git 的时候, 加上--global 是针对当前用户起作用的,如果不加, 那只对当前仓库起作用

每个仓库的配置文件放在.git/config

cat .git/config
# [core]
#     repositoryformatversion = 0
#     filemode = true
#     bare = false
#     logallrefupdates = true
#     ignorecase = true
#     precomposeunicode = true
# [remote "origin"]
#     url = git@github.com:michaelliao/learngit.git
#     fetch = +refs/heads/*:refs/remotes/origin/*
# [branch "master"]
#     remote = origin
#     merge = refs/heads/master
# [alias]
#     last = log -1

别名就在[alias]后面,要删除别名,直接把对应的行删掉即可。

而当前用户的 Git 配置文件放在用户主目录下的一个隐藏文件.gitconfig 中:

$ cat .gitconfig
# [alias]
#     co = checkout
#     ci = commit
#     br = branch
#     st = status
# [user]
#     name = Your Name
#     email = your@email.com

配置别名也可以直接修改这个文件,如果改错了,可以删掉文件重新通过命令配置。