起因

这个问题是在计划实测 GitHub 的 ForkPull Request 功能时出现的。实战练习至少需要两个 GitHub 帐号——
帐号 A:远程文档库 P 的创建者。
帐号 B:仓库 P 的参与者、合作方。

帐号 B 的操作流程如下:

  1. 从 A 在线叉取P到自己帐号下,记为P1;
  2. 下拉 P1 到本地,并产生新版本 P2;
  3. 同步 P2 到 B 的远程仓库;
  4. 向发起一个归并意向(pull request);

实验进行到第三步时出错,GitBash实测脚本如下(用户信息那行已省略):

$ git clone git@github.com:andong2007/Excel.git Demo
Cloning into 'Demo'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 14 (delta 3), reused 5 (delta 1), pack-reused 0
Receiving objects: 100% (14/14), done.
Resolving deltas: 100% (3/3), done.

$ cd Demo/; echo 'Rem demo comments' >> batchFormatTabels.txt

$ git status -s
 M batchFormatTabels.txt

$ git add -u; git status -s;
warning: LF will be replaced by CRLF in batchFormatTabels.txt.
The file will have its original line endings in your working directory
M  batchFormatTabels.txt

$ git commit -m 'modify a file'
[master 5656244] modify a file
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git push -u origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:andong2007/Excel.git
 ! [remote rejected] master -> master (permission denied)
error: failed to push some refs to 'git@github.com:andong2007/Excel.git'

$

根据最后两句的理解,因为权限问题导致远程同步失败。
当时本机的密钥配置如下:

对比项 \ 密钥密钥A(帐号 A 专用)密钥B(帐号 B 专用)
存放路径私钥:~\.ssh\id_rsa
公钥:~\.ssh\id_rsa.pub
私钥:~\.ssh\id_andong
公钥:~\.ssh\id_andong.pub
创建顺序先创建后创建
下拉时的
调用情况
被调用未被调用

由此可见,问题出在密钥 B 的前后不一致,因为本地按默认的密钥进行了 SSH 校验。具体调用哪个密钥需要手动配置。

解决方法

~\.ssh\下创建配置文件 config,内容如下:

#Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host github-zad
HostName github.com
User git
IdentityFile ~/.ssh/id_andong

要领:为不同的密钥配置不同的 HostIdentityFile 即可

用法

  1. 启用默认的 密钥A 克隆远程文档库:
$ git clone git@github.com:andong2007/Excel.git
  1. 启用新增的 密钥B 克隆远程文档库:
$ git clone git@github-zad:andong2007/Excel.git

或者直接:

$ git clone github-zad:andong2007/Excel.git

再次实测

andon@Sinosoft-Zhouad MINGW64 ~/Desktop/excelTest
$ git clone github-zad:andong2007/Excel.git Demo
Cloning into 'Demo'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 14 (delta 3), reused 5 (delta 1), pack-reused 0
Receiving objects: 100% (14/14), done.
Resolving deltas: 100% (3/3), done.

# ... 

$ git push -u origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github-zad:andong2007/Excel.git
   4d73186..f2046f9  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

$

参考资料

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐