把Git的服務器搭建好之後,我最困惑的就是Git是怎麼配置的,服務器配置的user.name和email有什麼用?學了git config之後有了一些理解。本文主要想講解一下關於git config的一些命令及其作用。
首先git config分爲三個等級,分別是system、global和local,加載配置文件時,其權限大小依次爲local > global > system,也就是說,當配置出現重複或者不一致時,若local中有則加載local的,若無則加載global的,以此類推。
首先查看git config配置,加一個--list即可,若追加--global則顯示global信息
hjf@hjf-virtual-machine:~/gittest$ git config --list --global
user.name=hjf
[email protected]
alias.st=status
pull.rebase=true
這些信息其實是用戶主目錄下的.gitconfig文件中的內容:
hjf@hjf-virtual-machine:~/gittest$ cat ~/.gitconfig
[user]
name = hjf
email = [email protected]
[alias]
st = status
[pull]
rebase = true
類似的,local也是如此,不過local加載的是git工作目錄下.git/config文件
hjf@hjf-virtual-machine:~/gittest$ git config --list --local
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[email protected]:/home/git/gittest/.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
hjf@hjf-virtual-machine:~/gittest$
hjf@hjf-virtual-machine:~/gittest$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = [email protected]:/home/git/gittest/.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
每次修改config信息時,也是會修改對應的config文件,例如
hjf@hjf-virtual-machine:~/gittest$ git config --list --local
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[email protected]:/home/git/gittest/.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name=hjf
[email protected]
hjf@hjf-virtual-machine:~/gittest$ git config --local user.name 'hujianfei'
hjf@hjf-virtual-machine:~/gittest$ git config --local user.email '[email protected]'
hjf@hjf-virtual-machine:~/gittest$ git config --list --local
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[email protected]:/home/git/gittest/.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name=hujianfei
[email protected]
hjf@hjf-virtual-machine:~/gittest$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = [email protected]:/home/git/gittest/.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[user]
name = hujianfei
email = [email protected]
添加別名:
git config還可以對一些命令進行別名,例如,git status可以查看狀態,我們可以對status進行添加別名st,效果如下:
hjf@hjf-virtual-machine:~/gittest$ git status
位於分支 master
您的分支與上游分支 'origin/master' 一致。
無文件要提交,乾淨的工作區
hjf@hjf-virtual-machine:~/gittest$ git config --global alias.st status
hjf@hjf-virtual-machine:~/gittest$ git st
位於分支 master
您的分支與上游分支 'origin/master' 一致。
無文件要提交,乾淨的工作區
從上述操作結果可以看出,進行別名之後,git st即可代替git status,這樣可以省下很多時間。
git log,這個命令可以看到git的記錄,而且對文件提交者的name和email都有表述。
hjf@hjf-virtual-machine:~/gittest$ git log
commit 29acf02a077ecd924fc4f8f62103bfe0562b54b8
Author: hujianfei <[email protected]>
Date: Tue Jun 5 23:16:22 2018 +0800
新增b.c
commit 84898cd17bbd1ec272fe6e7ed48d047fd4dbb305
Author: hjf <[email protected]>
Date: Tue Jun 5 22:59:30 2018 +0800
新增a.c