最近换电脑又要配置一堆 dotfiles,正好碰到了 git 配置相关的一些文件。 这里给大家分享一些小技巧,如果经常使用 git GUI 工具的话,这些技巧你可能没有碰到过, 不过在开发的时候了解它们会带来不少便利。
使用映射简化 git 命令
笔者一般是在命令行使用 git,所以映射了很多简化命令方便快速操作 git。 比如 ga 是 git add , gmg 是 git commit -m等。你可以根据自己的需求做一些映射放到你的bashrc/zshrc文件等,这样使用更快一些。
使用配置文件
配置文件(~/.gitignore)有非常多配置选项,日常只会用到很小一部分,不过有几个配置比较有用。这里举几个例子:
- 比如配置一个全局的 gitignore 文件忽略你自己的一些个人文件,经常开发的时候我会建立一些临时测试文件夹需要忽略掉, 但是又不想写到代码仓库的ignore里,这个时候就可以用一个自己的 gitignore_global
- 使用 includeIf 语法,你可以在一个文件夹下实现覆盖配置。比如公司电脑我在 work 文件下工作,提交者使用公司邮箱。 但是电脑上别的项目我又想使用自己的邮箱作为提交者,如果你不嫌麻烦一个个配置仓库的 gitconfig,就可以在 work 文件夹下 建立一个配置修改你的 user 为公司邮箱。
- 比如可以统一提交的模板信息,使用一个 gitmessage 文件用作模板,这样提交信息更清晰(下边介绍个命令行工具可以实现)
这就是我常用的几个配置,可以根据自己的需求来设置:
[user]
name = PeasusWang
email = XXX@qq.com
[core]
editor = vim
whitespace = fix,-indent-with-non-tab,trailing-space,cr-at-eol
excludesfile = ~/.gitignore_global
[color]
ui = auto
[color "branch"]
current = yellow bold
local = green bold
remote = cyan bold
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
whitespace = red reverse
[color "status"]
added = green bold
changed = yellow bold
untracked = red bold
[diff]
tool = vimdiff
[difftool]
prompt = false
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
[commit]
template = ~/.gitmessagegit 周边工具
另外基于 git 还有很多方便的工具,大家可以根据需求来安装,笔者之前接触过的有如下一些:
- tig: text-mode interface for git. 喜欢命令行的可以尝试下。 https://github.com/jonas/tig
- git-extras: 提供了很多方便的 git 工具,比如 git summary 可以输出代码的提交统计。https://github.com/tj/git-extras
- git-cz: https://github.com/commitizen/cz-cli 用来统一 git commmit 提交信息,代替 gitmessage。参考文章:https://juejin.im/post/5afc5242f265da0b7f44bee4
- pre-commit: git pre commit 工具,比如提交之前做一些检查或者单测。 https://pre-commit.com/
- gitignore.io: 搜索ignore文件模板。www.gitignore.io
GUI 工具
这些是笔者日常使用收集和整理的一些 git 技巧(有些甚至算不上啥技巧),如果老铁们有一些有用的技巧和工具也可以留言交流让更多人看到。
常用的 git 小技巧