您的位置:首页 > 其它

git config 常用配置

2018-01-10 10:42 471 查看
git config 常用配置

用了2年git了,除了当时入门时一周看完并基本掌握了 廖雪峰的Git教程 上面的知识,之后这2年中,在我们那个小Team里,我的
git 能力还算上流了,也就再也没有仔细看过 git 相关教程了。

近来换了份工作,熟悉新环境的这段时间又简单翻了下 git 相关教程,赫然发现自己知道的真是太少了,连最基本的 git config 有哪些,都有什么用都不清楚......


1、配置用户名和邮箱

在以往使用git时,都只接触到了下面这两组config命令:
# 查看或配置当前项目,config文件在项目目录下:.git/config
git config user.name [NAME]
git config user.email [EMAIL]

# 查看或配置当前用户,config文件在用户目录下:~/.gitconfig
git config --global user.name [NAME]
git config --global user.email [EMAIL]


却不知原来还有一组:
# 查看或配置当前系统,config文件在系统目录下:/etc/gitconfig
git config --system user.name [NAME]
git config --system user.email [EMAIL]


2、配置默认文本编辑器

git 在需要用户输入额外信息时,会调用系统指定的默认编辑器,一般可能是 vi 或者 vim。

如果你偏爱其他的编辑器,比如Emacs,那么就可以这样设置:
git config [--system|global] core.editor emacs


3、配置差异分析工具

会用git的人,肯定知道 git diff,但并不一定知道这个 diff 命令调用的差异分析工具也是可以改的,而且git能支持的差异分析工具很多,如:kdiff3、tkdiff、meld、xxdiff、emerge、vimdiff、gvimdiff、ecmerge、opendiff等等。

假如我们要改用vimdiff,我们可以这样设置:
git config [--system|global] merge.tool vimdiff


4、配置命令别名

我们在用git的时候,很多时候都是用的命令行形式的,不仅要记得住大量命令,还要能灵活进行组合,这就很是头大了,正因为此,命令别名的重要性就出来了。但与其说是别名,倒不如说是另类的简写形式。

别名的配置也需要使用config命令,比如给 git status 设置别名 st:
git config --global alias.st status


这样我们以后使用的时候,直接用 git st 就可以做 git status 的事了。

再来一个从 廖雪峰的Git教程 上看来的,很牛掰的,我自己也在用的一个别名:
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 的效果是这样的,惊不惊喜,意不意外?



20170605更新:

为了更方便查看,本人对上面的lg命令进行了简单的修改:
log --color --graph --pretty=format:'%C(bold red)%h%C(reset) - %C(bold green)(%cr)%C(bold blue)<%an>%C(reset) -%C(bold yellow)%d%C(reset) %s' --abbrev-commit


其中,

%h 表示提交id;

%cr 表示提交时间;

%an 表示提交人;

%d 表示 分支、tag、HEAD 等信息;

%s 表示提交的信息。

效果如下:




5、查看已有配置信息

要查看已经存在的配置信息,我们可以用如下命令:
git config [--system|global] --list


其中我的Windows版Git,执行git config --list是这个效果:
core.symlinks=false
core.autocrlf=input
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
rebase.autosquash=true
credential.helper=manager
gui.fontdiff=-family "Microsoft YaHei UI" -size 10 -weight normal -slant roman -underline 0 -overstrike 0
gui.encoding=utf-8
user.email=shawn194@qq.com
user.name=ShawnXia
alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
color.status=auto


如果要单独设置上面这些项,可以用前面几点提到的方法进行设置,比如:
# status 高亮模式为 auto
git config --global color.status auto
# branch 高亮模式为 auto
git config --global color.branch auto
# ui 高亮模式为 auto
git config --global color.ui auto


虽然 Git 是个强大的工具,但俗话说“磨刀不误砍材工”,我们在使用前对它进行一些合理的配置,将会大大提升工作效率。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git config 常用配置