1. 程式人生 > >git pull / push denied to xxx user

git pull / push denied to xxx user

1. 原由:我一臺電腦使用了兩個github的賬號,導致git push時候 一直用的一個祕鑰~/.ssh/id_rsa.pub。

            而github則明確規定,電腦ssh_key只能屬於一個賬號,不能多個賬號同時時候,因為他無法區分你使用哪個賬號進行操作

解決思路:

    生成另外一個新的ssh_key,

    新增ssh_key到github賬號後臺,

    給~/.ssh/config 新增一個新的Host,

    修改本地遠端倉庫地址。

一、新建一個額外的ssh_key 命名為123456,路徑:/Users/jack/.ssh/id_rsa_123456
user$ ssh-keygen -t rsa -C “[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/jack/.ssh/id_rsa): /Users/jack/.ssh/id_rsa_123456
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/jack/.ssh/id_rsa_123456.
Your public key has been saved in /Users/jack/.ssh/id_rsa_123456.pub.
The key fingerprint is:
SHA256:eLxHnDXEHd6HO8EpM1TFiG41MgZbuW2EWMhxEmAY9es 
[email protected]
The key's randomart image is: +---[RSA 2048]----+ | .++o=O**o=.| | .. .+BOo*+.| | oo*B*.o| | o . ++=oo.| | . S =. .o | | . + . | | . E | | . | | | +----[SHA256]-----+ 二、給config檔案新增一個Host為github-123456 由於~/.ssh沒有config檔案,因此我新建了一個,如果有config檔案,直接在config檔案最後一行 追加下面內容 user$ cd ~/.ssh user$ touch config user$ vi config 按i輸入以下內容 注意Host,以及File指定剛剛生成的在本機的路徑位置。.pub這個可以省略寫入 Host github-123456 HostName github.com User git IdentityFile ~/.ssh/id_rsa_123456 按Esc 在按:wq 三、修改專案的remote url倉庫地址 我的專案地址在~/code/nodejs user$ cd ~/code/nodejs user$ git remote -v origin https://github.com/haha/nodejs_learn_begin.git (fetch) origin https://github.com/haha/nodejs_learn_begin.git (push) 這裡能看到當前的fetch以及push的倉庫遠端地址, 需要修改他們[把上面的github.com改成剛剛config新增新的Host如github-123456] 注意下面origin 也就是上面列出的倉庫地址的origin,也就是同時修改fetch 以及push user$ git remote set-url origin https://github-123546/haha/nodejs_learn_begin.git user$ git remote -v origin github-123456:haha/nodejs_learn_begin.git (fetch) origin github-123456:haha/nodejs_learn_begin.git (push) user$ ssh -T github-123456 這時候提示 Hi 123456! You've successfully authenticated, but GitHub does not provide shell access. user$ git push origin master
接著就完成了!