在github上或者其他,新建一个仓库,打算将已经存在的仓库的一些/全部分支推送到新仓库中。

1) 在github上新建empty仓库;假设地址为 git@git.github.xxx.git

2) 进入到你本地的repo文件夹, 执行

git remote add repo_addr git@git.github.xxx.git

作用是增加远程仓库的地址,该地址用repo_addr 来表示; 默认情况下,你从远端git clone下来的仓库里面有一个默认的地址,以origin表示。执行git remote -v 可以看到所有的地址符号。

之所以在一个repo内存多个远程仓库的地址,是因为有时候需要跟不同的仓库打交道, 本例就是一个场景;

3) 假设你想将你本地repo的branch3分支推送到repo_addr对应的仓库,则执行:

git push -u repo_addr branch3.

不用-u或许也可以达到效果,感兴趣的可以深入查一下;

4)总结:

本质上就是不同仓库之间的交互操作。 关于git remote 的综合详细介绍,请直接参考官方文档:

https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

常用的git remote:

1) git remote

It lists the shortnames of each remote handle you’ve specified. If you’ve cloned your repository, you should at least see origin — that is the default name Git gives to the server you cloned from。

$ git remote
origin

2) git remote -v

You can also specify -v , which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote:

$ git remote -v
origin	https://github.com/schacon/ticgit (fetch)
origin	https://github.com/schacon/ticgit (push)

3) git remote add xxx addr, 上例已经用过。

其他连想:

git branch 指令:

git branch :

list all the branches that in you local repo;

git branch -r :

list all the branches that in the remote.  If you locally define origin, and repo_addr, just as the example above, this command can list all the branchs in the remote repos :

origin/xxx,  repo_addr/xxxx