git 配置文件位置;git配置文件設置

一. 配置文件的存儲位置

Git相關的配置文件有三個git

1. /etc/gitconfig:包含了適用於系統全部用戶和全部項目的值。vim

2.~/.gitconfig:只適用於當前登陸用戶的配置。ide

3. 位於git項目目錄中的.git/config:適用於特定git項目的配置。ui

對於同一配置項,三個配置文件的優先級是1<2<3spa


二. 一些有用的配置項

1. 設置別名

[alias] 爲git命令配置別名.net

例:code

[plain]  view plain  copy
  1. [alias]  
  2.     st = status  
  3.     ci = commit  
  4.     br = branch   

當你有了上述配置後,使用git st等同於使用git stautsorm


甚至有人喪心病狂的 設置 git lg 這種快捷方式:blog

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
這樣 git lg ,實際執行的是「
git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
」,效果仍是不錯的。


2. 輸出顏色

[color] 設置git輸出着色ip

例:

[plain]  view plain  copy
  1. [color]  
  2.     ui = true  

設置color.ui爲true來打開全部的默認終端着色。

對比一下,無此配置時

加入配置後



3. core.filemode 讓git忽略對文件權限的修改

[plain]  view plain  copy
  1. [core]  
  2.     filemode = false  

4.使用vimdiff呈現Git diff差別

[plain]  view plain  copy
  1. [diff]  
  2.     tool = vimdiff  
  3. [difftool]  
  4.     prompt = false  
  5. [alias]  
  6.     d = difftool  
使用時只需將用到git diff的地方換爲git d就能夠了。


三. 用git config操做配置文件

1. 列出當前配置項

git config [–system|–global|–local] -l
使用system, golbal, local時,分別列出對應一部分中的1,2,3三個文件之一的配置項。
若是不加上述三個選項,則會按一部分中所說的優先級合併全部配置項並輸出。

2.添加配置項 

git config [–local|–global|–system]  section.key value
例:
[plain]  view plain  copy
  1. git config core.filemode true   
執行後會在配置文件中添加 
[plain]  view plain  copy
  1. [core]  
  2.     filemode = true  

3.刪除配置項

git config [–local|–global|–system] –unset section.key