git配置文件读取顺序

时间:2024-10-03 17:07:27
  作者: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。最后载入的同名配置会覆盖前者载入的配置