同时将代码备份到Gitee和GitHub

同时将代码备份到Gitee和GitHub

如何将GitHub项目一步导入Gitee
如何保持Gitee和GitHub同步更新

如何将GitHub项目一步导入Gitee

方法一:

登陆 Gitee 账号,点击右上角的 + 号,点击「从 GitHub 导入项目」,在跳转的页面中授权 Gitee 访问。

然后选择性的从GitHub中导入

方法二:

在创建项目的时候,选择导入已有项目。

如果是私有项目将会需要输入 GitHub 的账号信息。

如何保持Gitee和GitHub同步更新

方式一: 使用Gitee的强制同步

我还是只用维护 github 那份源码, gitee 这边没忘记的话, 手搓点击下强制同步按钮即可。

但是容易忘记, 造成两边不完全同步。

切记,只有从GitHub上导入的仓库才有强制同步的按钮

方式二: 手搓 push 多次

那么不外乎就是配置多个远程库地址, 多次推送咯, 那么我们先来看看现有远程库的情况(以:WhiteHole

$ git remote --verbose
origin  https://gitee.com/BlackThompson/white-hole.git (fetch)
origin  https://gitee.com/BlackThompson/white-hole.git (push)

可以看到目前仅有 https://gitee.com/BlackThompson/white-hole.git 这个远程库地址.

我们来加一个 gitee 的远程地址, 首先在 gitee 建好同步仓库, 然后我们在本地添加一个新的远程库地址:

$ git remote add githuborigin https://github.com/BlackThompson/WhiteHole

添加完成后我们查看一下:

$ git remote --verbose
githuborigin    https://github.com/BlackThompson/WhiteHole (fetch)
githuborigin    https://github.com/BlackThompson/WhiteHole (push)
origin  https://gitee.com/BlackThompson/white-hole.git (fetch)
origin  https://gitee.com/BlackThompson/white-hole.git (push)

可以查看到以下2个远程库地址:

githuborigin: 是我们新加的 github 的远程库地址
origin: 是我们之前在 gitee 的远程库地址
接下来同步:

git add .
git commit -m "add gitee"
git push -u origin master
git push -u githuborigin master

比之前多个一次 git push 操作…其他和之前没有太大区别…没有更多的心智负担.

但是经常容易忘记…

方式三: 最多跑一次
不想着法偷懒的 coder 不是好程序员, 秉承 “最多跑一次” 的理念, 让我们试试怎么一次 push 统统搞定.

在本地 git 仓库里找到这个文件 .git/config, 内容如下:

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "origin"]
	url = https://gitee.com/BlackThompson/white-hole.git	
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[gui]
	wmstate = normal
	geometry = 1205x669+38+38 276 304
[remote "githuborigin"]
	url = https://github.com/BlackThompson/WhiteHole
	fetch = +refs/heads/*:refs/remotes/githuborigin/*

改为如下:

合并2个 remote 配置

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

上面这个手动配置是为了更好的说明而已, 其实可以用以下命令简化操作, 在 origin 节点下补充了一个新的远程地址.

$ git remote set-url --add origin https://github.com/BlackThompson/WhiteHole

看看补充后的远程地址情况

$ git remote --verbose
origin  https://gitee.com/BlackThompson/white-hole.git (fetch)
origin  https://gitee.com/BlackThompson/white-hole.git (push)
origin  https://github.com/BlackThompson/WhiteHole (push)

注意看后面的 (fetch)(push), 相信你会明白点什么.

然后我们可以继续这样使用来实现 github & gitee 的同步推送和分发:

git add .
git commit -m "github & gitee 同步推送和分发"
git push origin master

可以看到, 使用上和最初没有任何区别, 只是多配置了一次, 算是实现了 “最多配(跑)一次”.

方式四:命令行把GitHub上的代码拉取导入Gitee
如果是本地仓库,只在需要命令行添加用不同名称标识的 Gitee 和 Github 远程库。

git remote add 远程库名 远程库地址

具体方法操作如下:

1、首先通过 git remote -v 查看您要同步的仓库的远程库列表,如果在列表中没有您码云的远程库地址,您需要新增一个地址

git remote add 远程库名 远程库地址
eg: git remote add gitee git@github.com:xxx/xxx.git

如果在 add 的时候出现error: Could not remove config section ‘remote.xxx’.一类的错误,通过把仓库下.git/config 文件里面的 [remote “xxx”] 删掉或者是用别的远程库名即可。

2、从GitHub上拉取最新代码到本地

git pull 远程库名 分支名
eg:git pull origin master

3、推送本地最新代码到码云上

git push 远程库名 分支名
eg:git push gitee master

如果出现有差异的话需要自己手动解决差异

参考文章:

如何将 GitHub 项目导入码云?一步搞定!

如何同步多个 git 远程仓库
————————————————
原文链接:https://blog.csdn.net/qq_51771849/article/details/114179329

posted @ 2022-01-20 13:34  Earen  阅读(253)  评论(0编辑  收藏  举报