Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tarivs/39c79786f0c0092be6aa107843ec3f75 to your computer and use it in GitHub Desktop.
Save tarivs/39c79786f0c0092be6aa107843ec3f75 to your computer and use it in GitHub Desktop.
using multiple remote locations with Git

操作系统:windows10 将同一项目通过ssh提交到github.com 和 coding.net

Part 1 添加多个ssh

生成ssh公钥

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"  -f  ~/.ssh/id_rsa_coding
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"  -f  ~/.ssh/id_rsa_github

查看生成的公私钥

$ ls ~/.ssh/
id_rsa_coding.pub  id_rsa_github.pub
id_rsa_coding  id_rsa_github 

添加公钥 分别将id_rsa_coding.pub,id_rsa_github.pub添加到github.com 和 coding.net

使用ssh-add 添加私钥到系统

 ssh-add ~/.ssh/id_rsa_coding
 ssh-add ~/.ssh/id_rsa_github

若ssh-add出现错误:Could not open a connection to your authentication agent,则先执行命令:

ssh-agent bash

查看添加的私钥

$ ssh-add -l
4096 SHA256:fdasfafwaefwaf /xxx/.ssh/id_rsa_coding (RSA)
4096 SHA256:4fafewafefaffs /xxx/.ssh/id_rsa_github (RSA)

配置config

touch ~/.ssh/config

在~/.ssh/config添加以下内容,用户必须为:git

$ cat ~/.ssh/config
Host coding.net
Hostname git-ssh.coding.net
Port 443
User git
Identityfile ~/.ssh/id_rsa_coding

Host github.com
Hostname github.com
User git
Identityfile ~/.ssh/id_rsa_github

测试

ssh -vT git@github.com 将获得更多的信息

$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address 'xxx' to the list of known hosts.
Hi tom! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@coding.net
Hello tom You've connected to Coding.net by SSH successfully!

帮助资料

  1. coding 无法使用 22 端口的 SSH 服务怎么办? https://coding.net/help/faq/git/git.html#ssh-
  2. github publickey问题 https://help.github.com/articles/error-permission-denied-publickey/

Part 2 本地项目添加多个push remote

编辑项目下的config文件

$ vim ./.git/config

在[remote "origin"]中添加

url = git@github.com:tom/xxxProject.git

查看此时的config文件

$ cat ./.git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
        hideDotFiles = dotGitOnly
[gui]
        wmstate = normal
        geometry = 835x475+130+130 185 214
[remote "origin"]
        url = git@git.coding.net:tom/xxxProject.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = git@github.com:tom/xxxProject.git
[branch "master"]
        remote = origin
        merge = refs/heads/master


$ git remote -v
origin  git@git.coding.net:tom/xxxProject.git (fetch)
origin  git@git.coding.net:tom/xxxProject.git (push)
origin  git@github.com:tom/xxxProject.git (push)

上面的过程也可以通过以下命令完成

git remote set-url --add origin git@github.com:tom/xxxProject.git

帮助资料

  1. 推送git项目到多个远程仓库 http://blog.codepiano.com/2013/07/03/push-multi-remote-repositories/
  2. Git管理多个远程仓库(以Github和Coding为例) http://laker.me/blog/2015/09/17/15_0917_git_mutiply_remotes/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment