一、 全局配置
1. 配置文件
git全局配置文件.gitconfig默認在當前系統用戶文件夾下,window可運行%USERPROFILE%查找,Mac系統在cd ~查找。
具體配置可參考如下,其中:
【user】: 用戶提交時顯示在log裏的信息
【alias】: 常用git命令簡寫
【core】: window系統和類linux系統回車鍵轉換
【push】: 默認對應遠端(當本地分支名與遠程分支名不一致有用)
[user]
name = hoby
email = [email protected]
[alias]
st = status
co = checkout
br = branch
ci = commit
pl = pull --rebase
ps = push
mg = merge --no-ff
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
[core]
autocrlf = input
ignorecase = false
[push]
default = upstream
若要使用git mergetool功能,再增加配置(cmd處可修改成本地文件比較工具如beyond compare):
[merge]
tool = sourcetree
[diff]
tool = sourcetree
[difftool "sourcetree"]
cmd = 'C:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe' \"$LOCAL\" \"$REMOTE\"
[mergetool "sourcetree"]
cmd = 'C:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe' -base:\"$BASE\" -mine:\"$LOCAL\" -theirs:\"$REMOTE\" -merged:\"$MERGED\"
trustExitCode = true
2. 命令修改
如果不加--global 修改的是當前項目git配置。
$ git config --global --list // 查看全局配置 $ git config --global user.name "用戶名"
$ git config --global user.email "郵箱" $ git config --global alias.br branch // 修改簡寫 $ git config --unset alias.co // 刪除配置項 $ git config --global core.ignorecase false // 關閉忽略大小寫
二、 Git密鑰配置
1. 以SSH方式創建Git項目
1.)檢查SSH Key存在
如果存在id_rsa.pub 或 id_dsa.pub 文件,跳過此步。
$ cd /root/.ssh // linux默認在/root/.ssh下,windows在當前用戶下有個.ssh文件夾
$ ls
2.)創建SSH Key
創建ssh key時會提示自定名稱和push時的密碼(不是git登錄密碼),一般推薦略過,直接三個回車,如果創建成功會出來一個有圖案的小框框。
$ ssh-keygen -t rsa -C "[email protected]" // 此處email可任意,不一定要gitLab登錄郵箱
3.)查看SSH Key
copy公鑰內容到gitLab裏,添加進去。
$ cat ~/.ssh/id_rsa.pub
4.)測試SSH Key
$ ssh -T [email protected] //ssh -T [email protected] 或 ssh -T [email protected]
Welcome to xxxx, yourname! 出現這句話表示成功
5.)配置多個網站ssh密鑰
在生成每個網站ssh-key時,自定義名稱不要一樣,然後在~/.ssh目錄下新建一個config文件,然後配置多個網站的ssh信息,內容如下:
# gitLab
Host dev.gitLab.com
HostName dev.gitLab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# phabricator
Host 192.168.1.5
HostName 192.168.1.5
Port 22
PreferredAuthentications publickey
IdentityFile ~/.ssh/phabricator
2. 以HTTP(S)方式創建Git項目
http(s)創建的項目,操作時總是提示用戶名和密碼,不勝其煩,以下方案可以讓Git永遠記住用戶名與密碼:
1.)添加HOME變量
在Windows用戶變量中添加一個HOME環境變量,值爲%USERPROFILE%,如下圖:
2.)創建_netrc文件
在"開始>運行"中打開%Home%,新建一個名爲_netrc的文件,輸入Git服務器名、用戶名、密碼保存,可放多個不同登錄信息的Git項目,中間空一行即可,具體內容如下:
machine dev.github.com
login hoby
password 123456