# 将GitHub代码同步到Gitee上,并同步更新

# 创建Gitee仓库

我本来已经有GitHub仓库了,现在要在Gitee上创建,可以选择直接从GitHub上导入,如下:

这样,Gitee上就有跟GitHub上一样的代码了。

# 同步更新代码

先查看当前远程的路径是什么:

$ git remote -v
origin  git@github.com:LarryDpk/pkslow-samples.git (fetch)
origin  git@github.com:LarryDpk/pkslow-samples.git (push)

添加Gitee的地址:

$ git remote set-url --add origin git@gitee.com:larrydpk/pkslow-samples.git

再次查看就有两个地址了:

$ git remote -v
origin  git@github.com:LarryDpk/pkslow-samples.git (fetch)
origin  git@github.com:LarryDpk/pkslow-samples.git (push)
origin  git@gitee.com:larrydpk/pkslow-samples.git (push)

我们随便更新一个文件,然后提交:

$ git commit -m "add gitee url"
[master 4982dc5] add gitee url
 1 file changed, 3 insertions(+)

推送代码:

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 357 bytes | 357.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:LarryDpk/pkslow-samples.git
   3b350aa..4982dc5  master -> master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 357 bytes | 357.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:larrydpk/pkslow-samples.git
   3b350aa..4982dc5  master -> master

看日志就知道已经成功推送到两个远程的仓库了。

# 同步更新的另一种配置方式

除了命令行,我们还可以通过配置文件来实现:

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = git@github.com:LarryDpk/pkslow-samples.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = git@gitee.com:larrydpk/pkslow-samples.git
[branch "master"]
        remote = origin
        merge = refs/heads/master

我们还可以添加多个remote,这样可以单独推送:

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = git@github.com:LarryDpk/pkslow-samples.git
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@gitee.com:larrydpk/pkslow-samples.git
[remote "github"]
	url = git@github.com:LarryDpk/pkslow-samples.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[remote "gitee"]
	url = git@gitee.com:larrydpk/pkslow-samples.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

单独推送如下:

$ git remote -v
gitee   git@gitee.com:larrydpk/pkslow-samples.git (fetch)
gitee   git@gitee.com:larrydpk/pkslow-samples.git (push)
github  git@github.com:LarryDpk/pkslow-samples.git (fetch)
github  git@github.com:LarryDpk/pkslow-samples.git (push)
origin  git@github.com:LarryDpk/pkslow-samples.git (fetch)
origin  git@github.com:LarryDpk/pkslow-samples.git (push)
origin  git@gitee.com:larrydpk/pkslow-samples.git (push)

$ git push github
Everything up-to-date

$ git push gitee
Everything up-to-date

当然,上面的操作都基于配置了ssh key,且是同一个Key。


References:

https://developer.aliyun.com/article/907034

上次更新: 2023/8/18 23:39:36