Git学习==git config

    把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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章