1. 程式人生 > 實用技巧 >同時使用gitee和github

同時使用gitee和github

https://juejin.im/post/6844904180633600007

https://www.jianshu.com/p/68578d52470c

Git共有三個級別的config檔案,分別是:

  • system :%GitPath%\mingw64\etc\gitconfig檔案
  • global:$home.gitconfig檔案
  • local:%RepoPath%.git\config檔案

其中%GitPath%為Git的安裝路徑,%RepoPath%為某倉庫的本地路徑。

1. 刪掉全域性配置

git config --global --list
$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的郵箱"

2. 為不同賬戶配置ssh祕鑰

cd ~/.ssh 														# cd到當前使用者的.ssh資料夾
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "註冊gitee郵箱"		#為不同祕鑰指定名稱
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "註冊github郵箱"

完成後會在~/.ssh / 目錄下生成以下檔案:

  • id_rsa.github
  • id_rsa.github.pub
  • id_rsa.gitee
  • id_rsa.gitee.pub

複製公鑰分別在github和gitee中設定

cat id_rsa.github.pub
cat id_rsa.gitee.pub

新增新的私鑰

$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa.github
$ ssh-add ~/.ssh/id_rsa.gitee	

3. 進行全域性配置

touch ~/.ssh/config    

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.gitee

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.github

Host 它涵蓋了下面一個段的配置,我們可以通過他來替代將要連線的伺服器地址。 這裡可以使用任意欄位或萬用字元。 當ssh的時候如果伺服器地址能匹配上這裡Host指定的值,則Host下面指定的HostName將被作為最終的伺服器地址使用,並且將使用該Host欄位下面配置的所有自定義配置來覆蓋預設的/etc/ssh/ssh_config配置資訊。

Port 自定義的埠。預設為22,可不配置

User 自定義的使用者名稱,預設為git,可不配置

HostName 真正連線的伺服器地址

PreferredAuthentications 指定優先使用哪種方式驗證,支援密碼和祕鑰驗證方式

IdentityFile 指定本次連線使用的金鑰檔案

4. 測試連線

ssh -T [email protected]
Hi yourname! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T [email protected]
Hi yourname! You've successfully authenticated, but GITEE.COM does not provide shell access.

5. hexo部落格倉庫

vi .depoly_git/.git/config 增加

[user]
	name = username
	email = email

6. 針對不同的專案倉庫

增加本地配置,在每個倉庫的.git/config中進行配置不同的使用者,以及其他的配置資訊

$ git config --local user.name 'github/gitee賬號名'
$ git config --local user.email 'github/gitee賬號郵箱'

--global是在全域性配置檔案中設定

--local 是針對當前倉庫的專案進行設定