自定义Git
# 10.自定义Git
我们可以对Git做一些配置
# 配置别名
有没有经常敲错命令?比如git status
?status
这个单词真心不好记。
如果敲git st
就表示git status
那就简单多了,当然这种偷懒的办法我们是极力赞成的。
我们只需要敲一行命令,告诉Git,以后st
就表示status
:
$ git config --global alias.st status
当然还有别的命令可以简写,很多人都用co
表示checkout
,ci
表示commit
,br
表示branch
:
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch
2
3
以后提交就可以简写成:
$ git ci -m "bala bala bala..."
--global
参数是全局参数,也就是这些命令在这台电脑的所有Git仓库下都有用。
如果有空格,可以用字符串包住:
git config --global alias.logone "log --pretty=oneline"
同样的,这些配置也是在Git的配置文件里的(忘了的同学请回顾《安装和配置Git》):
[alias]
st = status
cm = commit -m
2
3
别名就在[alias]
后面,要删除别名,可以修改配置文件,删除对应的行删掉;或者使用命令:
$ git config --global --unset
如果想要查看所有别名,可以这样:
git config --list --show-origin | findstr alias
其中,findstr是Windows下过滤字符串的语法,在Mac和Linux下可以用grep
# 项目配置
在git中,我们使用git config 命令用来配置git的配置文件,git配置级别主要有以下3类:
1、仓库级别 local 【优先级最高】
2、用户级别 global【优先级次之】
3、系统级别 system【优先级最低】,使用方式和global类似:git config --system
配置Git的时候,加上--global
是针对当前用户起作用的,相关的配置文件在用户目录下。
如果不加--global
,那只针对当前的仓库起作用,配置文件都放在当前目录的.git/config
文件中:
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "gitee"]
url = git@gitee.com:peterjxl/LearnGit.git
fetch = +refs/heads/*:refs/remotes/gitee/*
[remote "github"]
url = git@github.com:Peter-JXL/LearnGit.git
fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
remote = gitee
merge = refs/heads/master
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 一些其他配置
有时候,我们拉取项目的时候,会遇到文件名过长导致无法拉取的情况:
$ git clone git@xxxx.git
......
error:unable to create file xxxx : Filename too long
fatal:unable to checkout working tree
warning:Clone succeeded,butcheckout failed
You can inspect what was checked out with ‘git status’ and retry the checkout with ‘git checkout -f HEAD’
......
2
3
4
5
6
7
8
git是可以支持创建4096长度的文件名,上诉问题在Unix系统和Mac系统中是不会出现的,这是在Windows系统中调用旧的api,支持长度260长度的文件名。允许较长的文件名这个设置在Windows系统中默认是关闭的。
此时,我们可这样配置:
$ git config --global core.longpaths true