git笔记-2-git常用配置文件
一、git config 文件
Git有一个工具被称为git config,它允许你获取和设置变量;这些变量可以控制Git的外观和操作的各个方面。这些变量以等级的不同可以被存储在三个不同的位置:
(1) /etc/gitconfig 文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项 --system 给 git config,它将明确的读和写这个文件。
(2) ~/.gitconfig 文件 :具体到你的用户。你可以通过传递--global 选项使Git明确的读或写这个特定的文件。
(3) .git/config 位于git目录的config文件,特定指向该单一的库。如果 git config 时不加 --system 也不加 --global 选项,那么只作用于当前的git版本库,配置产生的修改都体现在 .git/config 文件中
三个config文件是逐级覆盖的关系,具体的覆盖非具体的。
1. 用户标识配置
$ git config --global user.name "John Doe" //user.name就是对[user]下的name进行配置
$ git config --global user.email johndoe@example.com
$ cat ~/.gitconfig
[user]
email = johndoe@example.com
name = John Doe
...
这里的修改是针对这个用户的所有git版本库的,若不加 -- global 可以就是只针对某一个具体的版本库起作用,修改体现在 .git/config 下。
更多配置:
$ git config --global core.editor emacs //指定你的编辑器
$ git config --global merge.tool vimdiff //指定你的比较工具
$ git config --list //检查你的设置
$ git help config //获取帮助
2. 移除配置
git config --unset user.name
git config --unset --global user.name
git config --unset --system user.name
git config --remove-section color
3. 获取配置
git config get user.name 获取一个属性的值,当然也可以直接cat上面的config文件。
4. ~/.gitconfig 文件示例
[user] name = XXX email = XXX@YYY.com [alias] st = status ci = commit [commit] [core] editor = vim [color] ui = auto [push] default = matching [alias] st = status co = checkout ci = commit br = branch rh = reset --hard rs = reset --soft lo = log --oneline la = log --author lg = log --graph dc = diff --cached cp = cherry-pick dir = rev-parse --git-dir
补充:
bc = branch --contains //git branch --contains <hash> 查看包含hash提交的分支,只能看本地分支,不加看fetch到本地的远程分支。
二、~/.gitignore
.gitignore 配置忽略git版本库中的文件(夹)
*.[oa] # 忽略*.o和*.a文件
*.[bB] # 忽略*.b和*.B文件,但是 my.b 除外
!my.b
dbg # 忽略dbg文件和dbg目录
dbg/ # 只忽略dbg目录,不忽略dbg文件
dbg # 只忽略dbg文件,不忽略dbg目录
!dbg/
/dbg # 只忽略当前目录下的dbg文件和目录,子目录的dbg不在忽略范围内
三、.git/hooks/pre-commit
(1) 比如在此文件的最后加入如下命令,可以检查每笔提交的补丁是否符合规范
git diff --cached $against -- | ./scripts/checkpatch.pl --no-signoff -
四、服务器迁移
若变更服务器了,可以使用 scp 命令来同步git配置(包括私钥) 和其它一些配置
(1) 假设两台机器IP分别为:A.104.238.161.75,B.43.224.34.73,你要从A服务器变更为B服务器,那么需要将A服务器上与git相关的配置文件拷贝到B服务器相同目录下,那么可以在A服务器上执行:
scp -r ~/{.ssh,.netrc,.gitconfig} <your job number>@43.224.34.73:<home path>
posted on 2019-02-24 21:24 Hello-World3 阅读(3057) 评论(0) 收藏 举报
浙公网安备 33010602011771号