progit笔记:自定义Git
配置Git
/etc/gitconfig
系统级,传递 --system
选项给 git config
,它就会读写该文件。
~/.gitconfig
用户级,传递 --global
选项让 Git 读写该文件。
.git/config
仓库Git目录下,向 git config
传递 --local
选项。
客户端基本配置
可以通过 git config --list
查看当前配置
core.editor
# 修改默认的编辑器
$ git config --global core.editor emacs
commit.template
# 将 .gitmessage.txt 作为运行 git commit 时显示在你的编辑器中的默认信息
$ git config --global commit.template ~/.gitmessage.txt
core.pager
# 指定 log 和 diff 等命令所使用的分页器,设置成空字符串,关闭该选项:
$ git config --global core.pager ''
user.signingkey
# GPG 签署密钥设置为配置项
$ git config --global user.signingkey <gpg-key-id>
core.excludesfile
# 在所有的版本库中忽略掉某一类文件,.gitignore_global类似于全局生效的 .gitignore 文件
$ git config --global core.excludesfile ~/.gitignore_global
格式化与多余的空白字符
Windows 使用回车(CR)和换行(LF)两个字符来结束一行,而 macOS 和 Linux 只使用换行(LF)一个字符。
core.autocrlf
# Windows 系统上,检出代码时,换行会被转换成回车和换行:
$ git config --global core.autocrlf true
# Linux 或 macOS,检出代码时,把 core.autocrlf 设置成input 来告诉 Git 在提交时把回车和换行转换成换行,检出时不转换:
$ git config --global core.autocrlf input
# 开发仅运行在 Windows 上的项目,可以设置 false 取消此功能,把回车保留在版本库中:
$ git config --global core.autocrlf false
core.whitespace
默认被打开的三个选项是:blank-at-eol
,查找行尾的空格;blank-at-eof
,盯住文件底部的空行;space-before-tab
,警惕行头 tab 前面的空格。
默认被关闭的三个选项是:indent-with-non-tab
,揪出以空格而非 tab 开头的行(你可以用 tabwidth 选项控制它);tab-in-indent
,监视在行头表示缩进的 tab;cr-at-eol
,告诉 Git 忽略行尾的回车。
如果正准备应用的补丁存有特定的空白问题
# 让 Git 在应用补丁时发出警告:
$ git apply --whitespace=warn <patch>
# 让 Git 在打上补丁前自动修正此问题:
$ git apply --whitespace=fix <patch>
服务器端配置
receive.fsckObjects
# 设置 receive.fsckObjects 为 true 来强迫Git 会在每次推送生效前检查库的完整性
$ git config --system receive.fsckObjects true
receive.denyNonFastForwards
# 禁用强制更新推送(force-pushes):push 命令后加 -f 标志
$ git config --system receive.denyNonFastForwards true
receive.denyDeletes
有一些方法可以绕过 denyNonFastForwards 策略。其中一种是先删除某个分支,再连同新的引用一起推送回 该分支。
# 把 receive.denyDeletes 设置为 true 可以把这个漏洞补上:
$ git config --system receive.denyDeletes true
Git属性
可以针对特定的路径配置某些设置项,这样 Git 就只对特定的子目录或子文件集运用它们。 这些基于路径 的设置项被称为 Git 属性,可以在你的目录下的 .gitattributes
文件内进行设置(通常是你的项目的根目录)。
关键字展开
Git 属性提供了另一种方法:我们可以编写自己的过滤器来实现文件提交或检出时的关键字替换。
“smudge”过滤器会在文件被检出时触发
“clean”过滤器会在文件被暂存时触发
# .gitattributes 添加过滤器
date*.txt filter=dater
expand_date
过滤器脚本
#! /usr/bin/env ruby
data = STDIN.read
last_date = `git log --pretty=format:
"%ad" -1`
puts data.gsub('$Date$', '$Date: ' + last_date.to_s + '$')
设置检出和暂存过滤器,检出时调用expand_date
完成smudge操作,暂存文件时perl表达式完成clean操作
$ git config filter.dater.smudge expand_date
$ git config filter.dater.clean 'perl -pe
"s/\\\$Date[^\\\$]*\\\$/\\\$Date\\\$/"'
测试
$ echo '# $Date$' > date_test.txt
提交该文件,并再次检出,你会发现关键字如期被替换了:
$ git add date_test.txt .gitattributes
$ git commit -m "Testing date expansion in Git"
$ rm date_test.txt
$ git checkout date_test.txt
$ cat date_test.txt
# $Date: Tue Apr 21 07:26:52 2009 -0700$
注意:.gitattributes
文件会随着项目一起提交,而过滤器 (例如这里的 dater
)不会,所以过滤器有可能会失效。
Git钩子
钩子都被存储在 Git 目录下的 hooks
子目录中。 也即绝大部分项目中的 .git/hooks
。
客户端钩子
-
提交工作流钩子
pre-commit
prepare-commit-msg
commit-msg
post-commit
-
电子邮件工作流钩子
applypatch-msg
pre-applypatch
post-applypatch
-
其它客户端钩子
pre-rebase
post-rewrite
post-checkout
post-merge
pre-push
服务器端钩子
-
pre-receive
-
update
-
post-receive