git 多賬戶配置完整實現(xiàn)過程詳解
環(huán)境 MacOS
檢查是否已有全局賬戶設置
$ git config --global user.name $ git config --global user.email
如果都沒有返回值,就說明沒有已配置的 git 賬戶,否則請刪除賬戶信息:
$ git config --global --unset user.name "yourname" $ git config --global --unset user.email "youremail"
生成公鑰和秘鑰
$ ssh-keygen -t rsa -C "your_email1" $ ssh-keygen -t rsa -C "your_email2"
注意
生成過程中的 Enter file in which to save the key (/Users/faremax/.ssh/id_rsa):
一步驟請分別起不同的名。
查看生成的文件
ls -l ~/.ssh
-rw------- 1 faremax staff 1679 7 6 11:05 id_rsa_github -rw-r--r-- 1 faremax staff 400 7 6 11:05 id_rsa_github.pub -rw------- 1 faremax staff 1679 7 6 11:06 id_rsa_gitlab -rw-r--r-- 1 faremax staff 405 7 6 11:06 id_rsa_gitlab.pub -rw-r--r-- 1 faremax staff 602 12 6 2017 known_hosts
忽略2017年創(chuàng)建的幾個文件,可以看到兩個不同的公鑰和私鑰對已經(jīng)生成成功了。
分別在 GitHub 和 Gitlab 中錄入對應的公鑰
$ pbcopy < id_rsa_github.pub # 復制文件內(nèi)容
$ pbcopy < id_rsa_gitlab.pub
添加并識別私鑰
$ ssh-agent bash $ ssh-add ~/.ssh/id_rsa $ ssh-add ~/.ssh/id_rsa_github $ ssh-add ~/.ssh/id_rsa_gitlab
- 創(chuàng)建配置文件
$ touch ~/.ssh/config
在該文件中添加以下配置
#該文件用于配置私鑰對應的服務器 #Default gitlab user(email1@hostname.com) Host git@hostname.com HostName https://hostname.com User git IdentityFile ~/.ssh/id_rsa_gitlab ###################################### #Add github user(email1@hostname.com) Host git@github.com HostName https://github.com User git IdentityFile ~/.ssh/id_rsa_github
驗證連接Git
以下輸入輸出表示鏈接成功,如果提示權(quán)限問題,說明秘鑰和公鑰匹配除了問題,請檢查并重復上述步驟(使用 -vT
參數(shù)執(zhí)行下面命令查看錯誤信息)
$ ssh -T git@github.com Hi faremax! You've successfully authenticated, but GitHub does not provide shell access. $ ssh -T git@hostname.com Welcome to GIT, faremax!
以上就是git 多賬戶配置完整實現(xiàn)過程詳解的詳細內(nèi)容,更多關(guān)于git 多賬戶配置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在VSCode中如何配置Python開發(fā)環(huán)境
VScode是一個相當優(yōu)秀的IDE,具備開源、跨平臺、模塊化、插件豐富、啟動時間快、顏值高、可高度定制等等優(yōu)秀的特質(zhì),所以用VScode來編寫Python,也是相當?shù)暮糜玫摹K?,今天我們就來講講,怎么在VScode上配置Python開發(fā)環(huán)境。2020-01-01git版本回退方式(git?reset、git?revert、git?stash)
這篇文章主要介紹了git版本回退方式(git?reset、git?revert、git?stash),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04