不乱于心,不困于情。
不畏将来,不念过往。如此,安好。

项目迁移(Gitee->Github)的两种实现方式

随着github的普及和流行,我们习惯把代码托管到类似github的远程仓库中。但是由于网络等种种原因,github在国内的访问并不稳定。于是国内各种托管平台应运而生,比较知名的有coding,码云等。很多国内程序员会把代码托管到多个平台,兼顾稳定性和流行性,有时候我们会有项目迁移这种需求.

一.Git设置多远程仓库

我有下面两个仓库:
https://gitee.com/fsoooo/test.git
https://github.com/fsoooo/test.git

方法一: 使用 git remote add 命令

1.1#添加多个远程仓库

先添加第一个仓库:
git remote add origin https://gitee.com/fsoooo/test.git
再添加第二个仓库:
git remote set-url --add origin https://github.com/fsooo/test.git

如果还有其他,则可以像添加第二个一样继续添加其他仓库。

然后使用下面命令提交:
git push origin --all

打开.git/config,可以看到这样的配置:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = https://gitee.com/wangslei/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = https://github.com/fsoooo/test.git
[branch "master"]
    remote = origin
    merge = refs/heads/master

刚才的命令其实就是添加了这些配置。如果不想用命令行,可以直接编辑该文件,添加对应的url即可。

1.2# 查看远程仓库的情况

git remote
origin

git remote
origin  https://gitee.com/fsoooo/test.git (fetch)
origin  https://gitee.com/fsoooo/test.git (push)

1.3# 查看远程仓库情况。可以看到 github 远程仓库有两个 push 地址。

git remote -v
gitee  https://gitee.com/fsoooo/test.git (fetch)
gitee  https://gitee.com/fsoooo/test.git (push)
gitee  https://github.com/fsoooo/test.git (push)

1.4 # 推送远程分支

这种方法的好处是每次只需要 push 一次就行了(账号密码不同的话,可能会输两次账号密码)

方法二: 修改配置文件**

2.1# 添加配置模块

打开 .git/config 找到 [remote],添加对应的 模块 即可,效果如下:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = https://gitee.com/wangslei/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[remote "github"]
    url = https://github.com/fsoooo/test.git
    fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

2.2# 查看远程仓库的情况

git remote
origin
github

git remote
github  https://github.com/fsoooo/test.git (fetch)
github  https://github.com/fsoooo/test.git (push)

2.3# 再次查看远程仓库的情况,可以看到已经有两个远程仓库了。然后再使用相应的命令 push 到对应的仓库就行了。

git remote
github
gitee

git remote -v
github  https://github.com/fsoooo/test.git (fetch)
github  https://github.com/fsoooo/test.git (push)
gitee https://gitee.com/fsoooo/test.git (fetch)
gitee https://gitee.com/fsoooo/test.git (push)

2.4 # 推送远程分支

这种方法的缺点是每次要 push 两次

git push origin master
git push github master

关于 git pull

方法一在 push 的时候比较方便。但是在 pull 的时候只能从方法一中的第一个 url 地址拉取代码。而方法二则不存在这种问题(可能要解决冲突)。
所以,如果只进行 push 操作,推荐方法一,如果也要进行 pull 操作,推荐方法二。

赞(0) 打赏
未经允许不得转载:seo优化_前端开发_渗透技术 » 项目迁移(Gitee->Github)的两种实现方式

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏