自定义 Git

我们可以对 Git 做一些配置。

配置别名

有没有经常敲错命令?比如 git status​?status ​这个单词真心不好记。

如果敲 git st ​就表示 git status ​那就简单多了,当然这种偷懒的办法我们是极力赞成的。

我们只需要敲一行命令,告诉 Git,以后 st ​就表示 status​:

bash 复制代码
$ git config --global alias.st status

当然还有别的命令可以简写,很多人都用 co ​表示 checkout​,ci ​表示 commit​,br ​表示 branch​:

bash 复制代码
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch

以后提交就可以简写成:

shell 复制代码
$ git ci -m "bala bala bala..."

--global ​参数是全局参数,也就是这些命令在这台电脑的所有 Git 仓库下都有用。

如果有空格,可以用字符串包住:

bash 复制代码
git config --global alias.logone "log --pretty=oneline"

同样的,这些配置也是在 Git 的配置文件里的(忘了的同学请回顾《安装和配置 Git》):

bash 复制代码
[alias]
	st = status
	cm = commit -m

别名就在 [alias] ​后面,要删除别名,可以修改配置文件,删除对应的行删掉;或者使用命令:

bash 复制代码
$ git config --global --unset 

如果想要查看所有别名,可以这样:

bash 复制代码
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 ​文件中:

bash 复制代码
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "gitee"]
        url = [email protected]:peterjxl/LearnGit.git
        fetch = +refs/heads/*:refs/remotes/gitee/*
[remote "github"]
        url = [email protected]:Peter-JXL/LearnGit.git
        fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
        remote = gitee
        merge = refs/heads/master

一些其他配置

有时候,我们拉取项目的时候,会遇到文件名过长导致无法拉取的情况:

bash 复制代码
$ git clone [email protected]

......
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'
......

git 是可以支持创建 4096 长度的文件名,上述问题在 Unix 系统和 Mac 系统中是不会出现的,这是在 Windows 系统中调用旧的 api,支持长度 260 长度的文件名。允许较长的文件名这个设置在 Windows 系统中默认是关闭的。

此时,我们可这样配置:

bash 复制代码
$ git config --global core.longpaths true

相关推荐
李艺为3 小时前
Ubuntu下展锐刷机工具spd_dump使用说明
android·linux·git·ubuntu
掘根9 小时前
Git分支管理
git
极小狐9 小时前
极狐GitLab 项目 API 的速率限制如何设置?
大数据·运维·git·elasticsearch·gitlab
残月只会敲键盘10 小时前
Git 命令速查手册
大数据·git·elasticsearch
掘金沸点顶流11 小时前
同一台电脑配置多个 git 账户(github, gitee, gitlab等)
git
苦逼IT运维15 小时前
Git LFS 学习笔记:原理、配置、实践与心路历程
笔记·git·学习
jstart千语16 小时前
【版本控制】idea中使用git
java·ide·git·intellij-idea·intellij idea
王鑫的博客88616 小时前
git常用修改命令
linux·git
苍煜19 小时前
IDEA在Git提交时添加.ignore忽略文件,解决为什么Git中有时候使用.gitignore也无法忽略一些文件
git·elasticsearch·intellij-idea
小生不才yz1 天前
21. git apply
git