問題:同一臺(tái)電腦上面多個(gè)項(xiàng)目并且項(xiàng)目對(duì)應(yīng)的github
賬號(hào)不是同一個(gè)淆攻,但是你鏈接的時(shí)候默認(rèn)走默認(rèn)賬戶和默認(rèn)的ssh配置。下面兩種方法可以指定當(dāng)前項(xiàng)目使用那個(gè)賬號(hào)提交挑童。
方法1: 使用SSH密鑰
-
配置SSH密鑰: 首先刻蟹,確保你為每個(gè)GitHub賬戶生成了不同的SSH密鑰對(duì)。這個(gè)網(wǎng)上一大堆生成ssh key和配置公鑰的教程不懂的可以百度立宜。
# 為賬戶A生成SSH密鑰 ssh-keygen -t rsa -b 4096 -C "your.email@example.com" # 將SSH密鑰添加到SSH代理 ssh-add ~/.ssh/id_rsa_accountA # 在GitHub賬戶A的設(shè)置中添加SSH公鑰(~/.ssh/id_rsa_accountA.pub)到SSH keys
重復(fù)以上步驟為賬戶B生成另一個(gè)SSH密鑰,并將其添加到GitHub賬戶B臊岸。
-
配置SSH配置文件: 創(chuàng)建或編輯
~/.ssh/config
文件橙数,設(shè)置每個(gè)GitHub賬戶使用不同的密鑰。如果.ssh文件下面沒有config
文件就新建一個(gè)config
文件# 賬戶A的SSH配置 # accountA 換成你的github用戶名 Host github.com-accountA HostName github.com User git IdentityFile ~/.ssh/id_rsa_accountA # 賬戶B的SSH配置 # accountB 換成你的github用戶名 Host github.com-accountB HostName github.com User git IdentityFile ~/.ssh/id_rsa_accountB
在本地的Git倉庫目錄下扇单,將遠(yuǎn)程倉庫的URL修改為使用上述配置中的Host:
注意:git@github.com-accountA
默認(rèn)連接沒有-github用戶名
自己加上去 相當(dāng)關(guān)鍵
git remote set-url origin git@github.com-accountA:username/repo.git # 對(duì)于賬戶A的項(xiàng)目
git remote set-url origin git@github.com-accountB:username/repo.git # 對(duì)于賬戶B的項(xiàng)目
第一個(gè)方法使用 ssh
的方式就就完成了商模。
方法2: 使用Credential Helper (不推薦)
注意這會(huì)清空默認(rèn)的賬號(hào)密碼
注意這會(huì)清空默認(rèn)的賬號(hào)密碼
注意這會(huì)清空默認(rèn)的賬號(hào)密碼
當(dāng)你使用https
協(xié)議提交的和拉取的時(shí)候,會(huì)使用默認(rèn)賬號(hào)和存儲(chǔ)的默認(rèn)用戶名與密碼(windows=>控制面板->所有控制面板項(xiàng)->憑據(jù)管理器->windows憑據(jù)下面可查看當(dāng)前存儲(chǔ)的賬號(hào)密碼)蜘澜。
使用Git的Credential Helper可以確保在HTTP/HTTPS訪問時(shí)使用指定的憑據(jù)施流。
-
配置Credential Helper:
為賬戶A的項(xiàng)目配置Credential Helper:
git config credential.helper 'store --file ~/.git-credentials-accountA'
為賬戶B的項(xiàng)目配置Credential Helper:
git config credential.helper 'store --file ~/.git-credentials-accountB'
然后,在
~/.git-credentials-accountA
文件中存儲(chǔ)賬戶A的憑據(jù)鄙信,在~/.git-credentials-accountB
文件中存儲(chǔ)賬戶B的憑據(jù)瞪醋。protocol=https host=github.com username=your_username password=your_personal_access_token
your_username替換為你的GitHub用戶名,your_personal_access_token替換為你的GitHub Personal Access Token装诡。Personal Access Token可以在GitHub的Settings -> Developer settings -> Personal access tokens中生成银受。
清除全局憑據(jù)設(shè)置:確保沒有全局憑據(jù)設(shè)置覆蓋了項(xiàng)目級(jí)別的設(shè)置。在命令行中運(yùn)行以下命令鸦采,清除全局憑據(jù)設(shè)置
git config --global --unset credential.helper
注意這會(huì)清空默認(rèn)的賬號(hào)密碼
請(qǐng)確保執(zhí)行了以上步驟宾巍,并將每個(gè)項(xiàng)目的遠(yuǎn)程倉庫URL設(shè)置為對(duì)應(yīng)的SSH Host 或 Credential Helper 文件路徑,這樣Git就會(huì)使用正確的身份驗(yàn)證信息渔伯。如果問題依然存在顶霞,請(qǐng)確保你沒有在其他地方進(jìn)行了全局或者其他級(jí)別的覆蓋配置。