git配置文件读取顺序

  作者:zhanhailiang 日期:2014-11-03

git包含三个配置文件:

  • /etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用git config 时用‘ –system‘选项,读写的就是这个文件。
  • ~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用git config 时用‘ –global‘选项,读写的就是这个文件。
  • 当前项目的git 目录中的配置文件(也就是工作目录中的.git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以.git/config 里的配置会覆盖/etc/gitconfig 中的同名变量。

可以通过git config –list查看当前配置:

[root@~/wade/git/node-lessons]# git config --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=https://github.com/alsotang/node-lessons.git
branch.master.remote=origin
branch.master.merge=refs/heads/master

我们也可以通过strace来检验git配置文件的加载顺序:

[root@~/wade/git/node-lessons]# strace git config --list
execve("/usr/bin/git", ["git", "config", "--list"], [/* 27 vars */]) = 0
brk(0)                                  = 0x151a000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd66c000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=30968, ...}) = 0
mmap(NULL, 30968, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f82cd664000
close(3)                                = 0
open("/lib64/libz.so.1", O_RDONLY)      = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 !\340\3611\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=91096, ...}) = 0
mmap(0x31f1e00000, 2183696, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x31f1e00000
mprotect(0x31f1e15000, 2093056, PROT_NONE) = 0
mmap(0x31f2014000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14000) = 0x31f2014000
close(3)                                = 0
open("/lib64/libpthread.so.0", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340] \3611\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=145896, ...}) = 0
mmap(0x31f1200000, 2212848, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x31f1200000
mprotect(0x31f1217000, 2097152, PROT_NONE) = 0
mmap(0x31f1417000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17000) = 0x31f1417000
mmap(0x31f1419000, 13296, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x31f1419000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY)      = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0p\356\341\3601\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1926680, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd663000
mmap(0x31f0e00000, 3750152, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x31f0e00000
mprotect(0x31f0f8a000, 2097152, PROT_NONE) = 0
mmap(0x31f118a000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x18a000) = 0x31f118a000
mmap(0x31f118f000, 18696, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x31f118f000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd662000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd661000
arch_prctl(ARCH_SET_FS, 0x7f82cd662700) = 0
mprotect(0x31f2014000, 4096, PROT_READ) = 0
mprotect(0x31f1417000, 4096, PROT_READ) = 0
mprotect(0x31f118a000, 16384, PROT_READ) = 0
mprotect(0x31f0c1f000, 4096, PROT_READ) = 0
munmap(0x7f82cd664000, 30968)           = 0
set_tid_address(0x7f82cd6629d0)         = 25648
set_robust_list(0x7f82cd6629e0, 0x18)   = 0
futex(0x7fffa73c199c, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fffa73c199c, FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 1, NULL, 7f82cd662700) = -1 EAGAIN (Resource temporarily unavailable)
rt_sigaction(SIGRTMIN, {0x31f1205c60, [], SA_RESTORER|SA_SIGINFO, 0x31f120f710}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {0x31f1205cf0, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x31f120f710}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=10240*1024, rlim_max=RLIM_INFINITY}) = 0
brk(0)                                  = 0x151a000
brk(0x153b000)                          = 0x153b000
getcwd("/root/wade/git/node-lessons", 4096) = 28
stat(".git", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
access(".git/objects", X_OK)            = 0
access(".git/refs", X_OK)               = 0
lstat(".git/HEAD", {st_mode=S_IFREG|0644, st_size=23, ...}) = 0
open(".git/HEAD", O_RDONLY)             = 3
read(3, "ref: refs/heads/master\n", 255) = 23
read(3, "", 232)                        = 0
close(3)                                = 0
access("/etc/gitconfig", R_OK)          = 0
open("/etc/gitconfig", O_RDONLY)        = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd66b000
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f82cd66b000, 4096)            = 0
access("/root/.gitconfig", R_OK)        = 0
open("/root/.gitconfig", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd66b000
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f82cd66b000, 4096)            = 0
stat(".git", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
access(".git/config", R_OK)             = 0
open(".git/config", O_RDONLY)           = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=269, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd66b000
read(3, "[core]\n\trepositoryformatversion "..., 4096) = 269
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f82cd66b000, 4096)            = 0
access("/etc/gitconfig", R_OK)          = 0
open("/etc/gitconfig", O_RDONLY)        = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd66b000
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f82cd66b000, 4096)            = 0
access("/root/.gitconfig", R_OK)        = 0
open("/root/.gitconfig", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd66b000
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f82cd66b000, 4096)            = 0
access(".git/config", R_OK)             = 0
open(".git/config", O_RDONLY)           = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=269, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd66b000
read(3, "[core]\n\trepositoryformatversion "..., 4096) = 269
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f82cd66a000
write(1, "core.repositoryformatversion=0\n", 31core.repositoryformatversion=0
) = 31
write(1, "core.filemode=true\n", 19core.filemode=true
)    = 19
write(1, "core.bare=false\n", 16core.bare=false
)       = 16
write(1, "core.logallrefupdates=true\n", 27core.logallrefupdates=true
) = 27
write(1, "remote.origin.fetch=+refs/heads/"..., 56remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
) = 56
write(1, "remote.origin.url=https://github"..., 63remote.origin.url=https://github.com/alsotang/node-lessons.git
) = 63
write(1, "branch.master.remote=origin\n", 28branch.master.remote=origin
) = 28
write(1, "branch.master.merge=refs/heads/m"..., 38branch.master.merge=refs/heads/master
) = 38
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f82cd66b000, 4096)            = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
close(1)                                = 0
munmap(0x7f82cd66a000, 4096)            = 0
exit_group(0)                           = ?

由此可以git配置文件加载顺序为/etc/gitconfig → ~/.gitconfig → .git/config,最后加载的同名配置会覆盖前者加载的配置

时间: 2024-12-31 17:09:26

git配置文件读取顺序的相关文章

经验之谈-MySQL配置文件读取顺序问题!

原因: 用mysql命令可以连上服务端,但通常数据存储在什么地方,参数设置的是什么,这需要知道mysql使用的是哪个配置文件.(通常是my.cnf这个配置问题) 因此需要清除了解my.cnf加载顺序. 默认加载顺序: 路径 作用 /etc/my.cnf Global options /etc/mysql/my.cnf Global options (as of MySQL 5.1.15) SYSCONFDIR/my.cnf Global options $MYSQL_HOME/my.cnf Se

MySQL配置文件读取顺序

启动进程的参数 The MySQL server maintains many system variables that indicate how it is configured. Each system variable has a default value. System variables can be set at server startup using options on the command line or in an option file. Most of them

MySQL读取配置文件的顺序、启动方式、启动原理

一.MySQL读取配置文件的顺序 读取顺序:/etc/my.cnf > /etc/mysql/my.cnf > /usr/etc/my.cnf > ~/.my.cnf 命令验证:[[email protected] ~]# mysql --verbose --help | grep my.cnf order of preference, my.cnf, $MYSQL_TCP_PORT,/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.

cocos2d-x开发: 一切应该从配置文件读取开始

想要做一款完整的游戏,应该从配置文件读取开始.cocos2d-x本身提供了UserDefault来操作xml格式的配置文件,准确的说配置这模块引擎开发者已经考虑到了.但是xml格式包含大量无关的格式信息,对于开发者直接使用编辑器操作也不是很友好.我期望的配置文件应该具备两个特点,第一:格式简单.这是我一贯的*nix风格的要求,我喜欢只管的#注释以及key-value的配置方式.可能唯一需要考虑的是这样简单的配置方式会不会因为业务扩展而显得功能不足.这一点是可以放心的,那些都是业务逻辑相关的,选择

Git配置文件与git config命令

在Git配置文件中配置变量,可以控制Git的外观和操作的各个方面.通过git config命令可以获得和设置配置变量. 一.Git配置文件的位置 这些变量可以被存储在三个不同的位置: 1./etc/gitconfig 文件(系统级配置文件):包含了适用于系统所有用户和所有库的值.如果你传递参数选项’--system’ 给 git config,它将明确的读和写这个文件. 2.~/.gitconfig 文件 (用户级配置文件):具体到某个用户.如果你传递参数选项’--global’ 给 git c

C 构造一个 简单配置文件读取库

前言 最近看到这篇文章, json引擎性能对比报告 http://www.oschina.net/news/61942/cpp-json-compare?utm_source=tuicool 感觉技术真是坑好多, 显露的高山也很多. 自己原先也 对着 json 标准定义http://www.json.org/json-zh.html 写过一般json解析器, 1000行后面跟上面一对比, 真是弱鸡. 后面就看了其中吹得特别掉几个源码,确实有过人之处,深感 自己不足. 下载一些也在研究,发现看懂会

[spring源码学习]二、IOC源码——配置文件读取

一.环境准备 对于学习源码来讲,拿到一大堆的代码,脑袋里肯定是嗡嗡的,所以从代码实例进行跟踪调试未尝不是一种好的办法,此处,我们准备了一个小例子: package com.zjl; public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void sayHello

java --配置文件读取

该工具类对于java项目中配置文件读取很方便~ 示例代码: package com.lky.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; import org.apache.commons.logging.Log; import or

Smarty从配置文件读取的变量

从配置文件读取的变量 配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(下节将讲到) 第二种语法在变量作为属性值并被引号括住的时候非常有用. (译注:举个例子 {include file="#includefile#"} 这样#includefile#将被当作字符处理,而不表示配置文件变量, 但可以这样表示 {include file="`$smarty.config.includefile`"