考虑到github不能免费建立私有仓库缘由,最近开始在使用码云托管项目,这样避免了链接数据库的用户密码等信息直接暴露在公共仓库中。今天忽然想到一个点,就是能不能同时把代码推送到github和码云上呢?答案是能够的。git
首先,咱们在开始一个项目时,在本地写了一些代码,须要同时托管到github和码云(gitee)上。这个时候咱们要怎么办呢?请接着看。github
在C:\Users\robin.ssh目录下运行git bash数据库
// 这个是给github生成的 ssh-keygen -t rsa -C "1148121254@qq.com" // 这个是给码云生成的 ssh-keygen -t rsa -C "cumtrobin@163.com"
生成后自行命名管理,这里再也不赘述。接着把公钥分别放在github和码云上。私钥能够用config文件管理bash
# 配置github.com Host github.com HostName github.com IdentityFile C:\\Users\\robin\\.ssh\\id_rsa_github PreferredAuthentications publickey User cumtRobin # 配置gitee.com Host gitee.com HostName gitee.com IdentityFile C:\\Users\\robin\\.ssh\\id_rsa_gitee PreferredAuthentications publickey User Tusi
接着咱们测试一下ssh
ssh -T git@github.com ssh -T git@gitee.com
成功则会获得这样的反馈测试
首先是在github和码云上分别建立一个仓库。这个玩过github的都知道,不细说。this
接着在本地项目根目录建立git仓库加密
git init
要把两个remote仓库与本地git仓库关联起来,咱们直接来运行url
// 添加github的远程库 git remote add origin git@github.com:cumtRobin/BlogFrontEnd.git // 添加码云的远程库 git remote add gitee git@gitee.com:tusi/BlogFrontEnd.git
而后咱们运行git remote查看添加的远程库列表命令行
git remote // 获得如下值 origin gitee
说明已经添加成功,接着咱们分别查看git status,会看到本地有不少文件待提交,接着git add, git commit,最后git push的时候要注意分开push
// push到github主分支 git push origin master // push到gitee主分支 git push gitee master
虽然麻烦了一点,须要push两次,可是目的是初步达成了。若是想要一次性push解决,那也不是没有办法。
为了不引发歧义,这里先将origin,gitee的remote库删除
git remote rm origin git remote rm gitee
从新添加remote
git remote add all git@github.com:cumtRobin/BlogFrontEnd.git
能够看到,我实际上是添加的github的远程库,只不过把它的名字叫作all。接着咱们把码云上的remote库也关联起来。
git remote set-url --add all git@gitee.com:tusi/BlogFrontEnd.git
这样操做之后,就能够运行一条push命令了
git push all --all
有人说能够改.git/config文件实现。其实刚才上面的命令修改的就是config文件,可是本人建议,多练练命令行,这样也会加深对git的理解。这时候咱们再查看一下.git/config文件。能够看到remote all下面是有两个url的。
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "all"] url = git@github.com:cumtRobin/BlogFrontEnd.git url = git@gitee.com:tusi/BlogFrontEnd.git
学会了两个托管平台的配置,那使用更多的托管平台也就不难实现了。
ps:再分享一个小技巧,因为我在生成ssh密钥时,加了passphrase,致使我每次push都要输入密码,很烦人。
其实,只要重置一下这个passphrase就能够了。
// 进入到.ssh目录,运行git bash ssh-keygen -p // 再输入密钥名,如id_rsa_github,先输入旧密码,而后一路回车便可,多个密钥重复此操做便可。
2019-04-18
git pull
的细节由于都是从本地 push
代码到远程仓库,好久没有从远程仓库拉取代码了,今天不当心在 github
上改了仓库中的 readme
文件,致使和 gitee
不一样步。使用 git pull
报错,慌的一批。
$ git pull There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=all/<branch> master
原来是要使用下面这条命令才行。
$ git pull all master From github.com:cumtRobin/BlogFrontEnd * branch master -> FETCH_HEAD Already up to date.
上面的 all
是指 remote
,即远程仓库,master
是指分支名,master
即主干分支。