自定義 Git

peterjxl發表於2024-09-27

我們可以對 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

以後提交就可以簡寫成:

$ git ci -m "bala bala bala..."

--global ​引數是全域性引數,也就是這些命令在這臺電腦的所有 Git 倉庫下都有用。

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

git config --global alias.logone "log --pretty=oneline"

同樣的,這些配置也是在 Git 的配置檔案裡的(忘了的同學請回顧《安裝和配置 Git》):

[alias]
	st = status
	cm = commit -m

別名就在 [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

一些其他配置

有時候,我們拉取專案的時候,會遇到檔名過長導致無法拉取的情況:

$ 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’
......

git 是可以支援建立 4096 長度的檔名,上述問題在 Unix 系統和 Mac 系統中是不會出現的,這是在 Windows 系統中呼叫舊的 api,支援長度 260 長度的檔名。允許較長的檔名這個設定在 Windows 系統中預設是關閉的。

此時,我們可這樣配置:

$ git config --global core.longpaths true

相關文章