GIT码农的世界程序员

「简单Git」config

2017-09-06  本文已影响185人  吴小傲
2017-12-29 重新规定格式,添加diff
2017-09-06 创建文章

概要

用于获取并设置仓库或全局或系统选项。这些变量可以控制 Git 的外观和操作的各个方面。

git config --local key value

wuaodeMacBook-Pro:SimpleGit wuao$ git config --local user.name "wuao"
wuaodeMacBook-Pro:SimpleGit wuao$ git config --local user.email 215731932@qq.com  # 可以省去引号

可以省略 --local

wuaodeMacBook-Pro:SimpleGit wuao$ git config user.name wuao
wuaodeMacBook-Pro:SimpleGit wuao$ git config user.email 215731932@qq.com
wuaodeMacBook-Pro:SimpleGit wuao$ git config --global user.name 吴奡
wuaodeMacBook-Pro:SimpleGit wuao$ git config --global user.email 215731932@qq.com
wuaodeMacBook-Pro:SimpleGit wuao$ git config --system user.name wuao
error: could not lock config file /etc/gitconfig: Permission denied  # 无法锁定配置文件,权限被拒绝。
wuaodeMacBook-Pro:SimpleGit wuao$ git config --system user.email 215731932@qq.com
error: could not lock config file /etc/gitconfig: Permission denied
级别 配置文件位置 优先级
仓库级 .git/config(每个仓库 Git 目录下) 一级
全局级 ~/.gitconfig 二级
系统级 /etc/gitconfig 三级

git config --local key

查看特定关键字对应级别的值。

wuaodeMacBook-Pro:SimpleGit wuao$ git config --local --get user.name  
wuao

可以省略 --local--get

wuaodeMacBook-Pro:SimpleGit wuao$ git config user.name
wuao
wuaodeMacBook-Pro:SimpleGit wuao$ git config --global user.name
吴奡

git config --local -l

wuaodeMacBook-Pro:SimpleGit wuao$ git config --local -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
user.name=wuao
user.email=215731932@qq.com
wuaodeMacBook-Pro:SimpleGit wuao$ git config --global --list
core.excludesfile=/Users/wuao/.gitignore_global
core.autocrlf=input
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
user.email=215731932@qq.com
user.name=吴奡
commit.template=/Users/wuao/.stCommitMsg
wuaodeMacBook-Pro:SimpleGit wuao$ git config --system -l
fatal: unable to read config file '/etc/gitconfig': No such file or directory  # 无法读取配置文件,没有这样的文件或目录。

git config -l

查看当前生效的配置,这个时候会显示最终三个配置文件计算后的配置信息。可能会看到一个关键字出现多次,如这里的 user.name 就有两个值,这是因为 Git 从不同的配置文件中读取相同的关键字。在这种情况下,对每个唯一的关键字,Git 会使用最后的那个值。

wuaodeMacBook-Pro:SimpleGit wuao$ git config -l
credential.helper=osxkeychain
core.excludesfile=/Users/wuao/.gitignore_global
core.autocrlf=input
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
user.email=215731932@qq.com # 全局里面的配置
user.name=吴奡  # 全局里面的配置
commit.template=/Users/wuao/.stCommitMsg
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
user.name=wuao  # 仓库里面的配置
user.email=215731932@qq.com # 仓库里面的配置

git config --local -e

执行这个命令的时候,Git 会用配置文件中设定的编辑器打开配置文件。

wuaodeMacBook-Pro:SimpleGit wuao$ git config --local -e
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[user]
        name = wuao
        email = 215731932@qq.com
[site]
~
~
~
"~/Desktop/document/SimpleGit/.git/config" 11L, 190C
wuaodeMacBook-Pro:SimpleGit wuao$ git config --global --edit
[core]
        excludesfile = /Users/wuao/.gitignore_global
        autocrlf = input
[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
[user]
        email = 215731932@qq.com
        name = 吴奡
[commit]
        template = /Users/wuao/.stCommitMsg
~
~
~
"~/.gitconfig" 14L, 415C

git config --local --add section.key value

添加配置项,注意 --add 后面的 sectionkeyvalue 一项都不能少,否则添加失败。

wuaodeMacBook-Pro:SimpleGit wuao$ git config --local --add site.url http://wuao.site/
wuaodeMacBook-Pro:SimpleGit wuao$ git config --local -e
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[user]
        name = wuao
        email = 215731932@qq.com
[site]
        url = http://wuao.site/  # 新增的配置
~
~
~
"~/Desktop/document/SimpleGit/.git/config" 14L, 229C

git config --local --unset section.key

删除指定配置项

wuaodeMacBook-Pro:SimpleGit wuao$ git config --local --unset site.url
wuaodeMacBook-Pro:SimpleGit wuao$ git config --local -e
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[user]
        name = wuao
        email = 215731932@qq.com
[site]  # 移除了配置
~
~
~
"~/Desktop/document/SimpleGit/.git/config" 12L, 197C

git config --local --remove-section section

删除 section

wuaodeMacBook-Pro:SimpleGit wuao$ git config --remove-section site
wuaodeMacBook-Pro:SimpleGit wuao$ git config --local -e
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[user]
        name = wuao
        email = 215731932@qq.com
~
~
~
"~/Desktop/document/SimpleGit/.git/config" 10L, 183C

git config --global diff.tool bc3

配置差异对比工具

wuaodeMacBook-Pro:SimpleGit wuao$ git config --global -l
core.excludesfile=/Users/wuao/.gitignore_global
core.autocrlf=input
difftool.sourcetree.cmd=/usr/local/bin/bcomp "$LOCAL" "$REMOTE"
difftool.sourcetree.path=-ro
mergetool.sourcetree.cmd=/usr/local/bin/bcomp "$LOCAL" "$REMOTE" "$BASE" "$MERGED"
mergetool.sourcetree.trustexitcode=true
user.email=215731932@qq.com
user.name=吴奡
commit.template=/Users/wuao/.stCommitMsg
wuaodeMacBook-Pro:SimpleGit wuao$ git config --global diff.tool bc3
wuaodeMacBook-Pro:SimpleGit wuao$ git config --global -l
core.excludesfile=/Users/wuao/.gitignore_global
core.autocrlf=input
difftool.sourcetree.cmd=/usr/local/bin/bcomp "$LOCAL" "$REMOTE"
difftool.sourcetree.path=-ro
mergetool.sourcetree.cmd=/usr/local/bin/bcomp "$LOCAL" "$REMOTE" "$BASE" "$MERGED"
mergetool.sourcetree.trustexitcode=true
user.email=215731932@qq.com
user.name=吴奡
commit.template=/Users/wuao/.stCommitMsg
diff.tool=bc3

git config --global mergetool.keepBackup false

在使用 git megetool 来解决冲突后,会生成备份文件 *.orig,大多数情况下不是我们想要的,在终端中配置这样就不会每次在解决冲突后生成对应的 .orig 文件了

查看更多「简单Git」

上一篇 下一篇

猜你喜欢

热点阅读