一、概述
依据配置文件的不同,可以将git的配置分为版本库级别的配置、全局配置(用户级别的配置)、系统级别的配置。这三种配置级别逐次降低,系统级别的配置优先级最低,优先级高的配置会覆盖优先级低的配置。为了更好的理解这些配置,比如,我的项目在/home/dq/project/git_test目录下,那么这三种配置文件路径分别如下:
版本库级别的配置
文件:/home/dq/project/git_test/.git/config
打开方式
git config -e
全局配置(用户级别的配置)
文件:/home/dq/.gitconfig
打开方式
git config -e --global
系统级别的配置
文件:/etc/gitconfig
打开方式
git config -e --system
当然,针对每种配置文件,除了git提供的打开方式,我们也可以使用vim直接打开,毕竟是文件嘛。
文件配置操作
git配置文件采用的是ini文件格式,git config命令,可以用于读取和修改ini配置文件的内容。读取配置的格式如下:
git config <section>.<key>
比如,我从配置文件中,读取filemode,使用git config core.filemode
修改配置的格式如下:
git config <section>.<key> value
如,我将filemode修改为false,使用git config core.filemode false
二、用户配置
主要包括用户名与邮箱的配置,如下
git config --global user.name "dq" git config --global user.email dq@findme.wang
三、别名配置
设置别名,主要是用于简化操作,将一个比较长的命令,用一个简单的字符代替。
配置别名的格式如下:
git config alias.name "command"
比如,我们要查看当前版本库的状态使用git status命令,为了简化,我们设置别名git st来查看版本库的状态,如下:
其实,我们发现,设置别名,也是修改配置文件。当然为了使设置的别名,对当前用户的所有项目都有效,推荐加上--global,使用如下格式
git config --global alias.name "command"
当然,如果一下子配置多条,我们也可以直接编辑配置文件,而不用一条条的去写,如下,修改提交命令
那么我就可以使用git cm "alias test"来提交
当然,一般我们可以在直接家目录添加一下git的配置,这样每个项目级别的配置都会继承用户级别的配置,我的配置如下:
#全局的配置 # core {{{ [core] quotepath = false autocrlf = false #自动转换换行符 repositoryformatversion = 0 filemode = true #文件权限 bare = false logallrefupdates = true ignorecase = false editor = /usr/bin/vim excludesfile = /Users/npaolucci/.gitignore_global pager=less -x4 preloadindex = true fscache = true safecrlf = true #}}} # color {{{ [color] ui = true [color "diff"] meta = yellow bold frag = magenta bold old = red bold new = green bold #[color] # branch = auto # diff = auto # status = auto [color "branch"] # current = red reverse local = white remote = blue [color "diff"] meta = yellow frag = magenta old = red bold new = green plain = white [color "status"] added = green changed = red untracked = cyan # }}} # push/pull/diff/options {{{ [apply] whitespace = nowarn #}}} # difftools {{{ [difftool "sourcetree"] cmd = opendiff \"$LOCAL\" \"$REMOTE\" path = [mergetool "sourcetree"] cmd = /Applications/SourceTree.app/Contents/Resources/opendiff-w.sh \"$LOCAL\" \"$REMOTE\" -ancestor \"$BASE\" -merge \"$MERGED\" trustExitCode = true # }}} # alias {{{ [alias] st = status -s cl = clone ci = commit -s cma = "!f() { curBranch=$(git symbolic-ref --short -q HEAD) && git commit -s -a -m \"$curBranch:$1\"; }; f" cm = "!f() { curBranch=$(git symbolic-ref --short -q HEAD) && git commit -s -v -m \"$curBranch:$1\"; }; f" ca = commit --amend amend = commit --amend caa = commit -a --amend -C HEAD filelog = log -u fl = log -u ai = add --interactive co = checkout br = branch #"!git branch -ra | grep -v done" bra = branch -ra #list commands lg1 = "log --all --pretty=oneline --decorate=full --graph --branches --abbrev-commit" #原来的 # lg2 = "log --all --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)—%an%C(reset)%C(bold yellow)%C(auto)%d%C(reset)' --abbrev-commit --date=relative --show-signature --branches" lg2 = "log --all --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(bold white)%s%C(reset) %C(yellow)—%an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative --branches" lg21 = "log --all --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(bold white)%s%C(reset)%C(yellow) |%an%C(reset)%C(bold yellow)%C(auto)%d%C(reset)' --abbrev-commit --date=relative --branches" #可以显示日期 年月日时分秒 lgt = "log --graph --all --branches --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ai)%C(reset) %C(white)%s%C(reset) %C(auto)%d%C(reset)'" # 里面加入的东西最多了 两种日期都显示 ,名字显示,还有那个分支的颜色稍微有些不同,可以明确的看出来是哪个分支的 lgall ="log --graph --all --branches --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(bold green)(%ai)%C(reset) %C(white)%s%C(reset) %C(yellow) - %an%C(reset)%C(auto) %d%C(reset)'" # no merge lgnm = "log --graph --all --branches --no-merges --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(auto)%d%C(reset)'" lg = "log --graph --all --branches --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(auto)%d%C(reset)'" le = log --oneline --decorate ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat ls1 = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate lds = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative lc = "!f() { git ll "$1"^.."$1"; }; f" lnc = log --pretty=format:"%h\\ %s\\ [%cn]" #list all aliases la = "!git config -l | grep alias | cut -c 7-" #d = diff --word-diff d = ! "echo '***********************************************************************' && git diff --word-diff --stat && echo '' && echo ======================================================================= && git diff --word-diff" #df = diff df = ! "echo '***********************************************************************' && git diff --stat && echo '' && echo ======================================================================= && git diff" dc = ! "echo '***********************************************************************' && git diff --cached --stat && echo '' && echo ======================================================================= && git diff --cached" #dc = diff --cached #list modified files in last commit dl = "!git ll -1" #diff last commit dlc = diff --cached HEAD^ dr = "!f() { git diff -w "$1"^.."$1"; }; f" diffr = "!f() { git diff "$1"^.."$1"; }; f" branchall = branch -ra #reset commands r = reset r1 = reset HEAD^ r2 = reset HEAD^^ rh = reset --hard rh1 = reset HEAD^ --hard rh2 = reset HEAD^^ --hard #git svn svnf = svn fetch svnr = svn rebase svnd = svn dcommit svnl = svn log --oneline --show-commit #stash sl = stash list sa = stash apply ss = stash save cp = cherry-pick grep = grep -Ii gr = grep -Ii #grep from root folder gra = "!f() { A=$(pwd) && TOPLEVEL=$(git rev-parse --show-toplevel) && cd $TOPLEVEL && git grep --full-name -In $1 | xargs -I{} echo $TOPLEVEL/{} && cd $A; }; f" #grep on filename f = "!git ls-files | grep -i" #rename branch tree to done- #done = "!f() { git branch | grep "$1" | cut -c 3- | grep -v done | xargs -I{} git branch -m {} done-{}; }; f" #assume aliases assume = update-index --assume-unchanged unassume = update-index --no-assume-unchanged #show assumed files assumed = "!git ls-files -v | grep ^h | cut -c 3-" #unassume all the assumed files unassumeall = "!git assumed | xargs git update-index --no-assume-unchanged" assumeall = "!git st -s | awk {'print $2'} | xargs git assume" lasttag = describe --tags --abbrev=0 lt = describe --tags --abbrev=0 #merges ours = "!f() { git co --ours $@ && git add $@; }; f" theirs = "!f() { git co --theirs $@ && git add $@; }; f" #list remotes rem="!git config -l | grep remote.*url | tail -n +2" #apply patch from other local repo lap = "!f() { git --git-dir=$1 format-patch -k -1 --stdout $2 | git am -3 -k; }; f" #subtree add sba = "!f() { git subtree add --prefix $2 $1 master --squash; }; f" # initial empty commit empty = "!git commit -am\"[empty] Initial commit\" --allow-empty" #add bitbucket remote ra = "!f() { git remote add $1 https://bitbucket.org/$2.git; }; f" #merge to 合并到master合并到dev mtomaster =! "currBranch=$(git status -b|grep 'On branch') && currBranch=${currBranch/'On branch'/} &&git checkout master && git merge --no-ff $currBranch -m \"Merge branch '"$currBranch"' \" && git push && git checkout $currBranch" #合并到dev mtodev =! "currBranch=$(git status -b|grep 'On branch') && currBranch=${currBranch/'On branch'/} &&git checkout develop && git merge --no-ff $currBranch -m \"Merge branch '"$currBranch"' to develop \" && git push && git checkout $currBranch" #自定义合并到那里 mergeto =! "currBranch=$(git status -b|grep 'On branch') && currBranch=${currBranch/'On branch'/} &&git checkout $1 && git merge --no-ff $currBranch -m \"Merge branch '"$currBranch"' to $1 \" && git push && git checkout $currBranch && echo $currBranch--合并到了==$1" bs = ! "echo '***********************************************************************' && git branch -vv && echo '' && echo ======================================================================= && echo '' && git status -s && echo '' && echo '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&' && echo '' " # }}} [rerere] enabled = true [status] submodulesummary = false [user] name = dq email = dequanLi_edu@126.com
四、自动补全
此外git官方也提供了关于git的相关配置,我比较喜欢的是里面的自动补全,我们可以下载使用,如下图(注意,首先得使用git --version看看自己git的版本,下载合适自己的版本)
网址:https://github.com/git/git/tree/v2.14.2
下载后,放置到根目录下,添加在.bashrc中添加如下代码:
source ~/.git-completion.bash
当然,此时并没立即生效,需要重新登录方可生效,当然我们可以在添加后执行 source .git-completion.bash ,使添加的配置立即生成。
若您感觉本站文章不错,读后有收获,不妨赞助一下?
我要赞助