git同步代码至github和gitee(码云)

我们有时候开发代码需要把代码同步到多个远程库中,如何操作才能做到呢?

我们知道,git是分布式版本控制系统,同步到多个远程库时,需要用不同的名称来标识不同的远程库,而git给远程库起的默认名称是origin。所以我们需要修改、配置名称,以关联不同远程库。有两种方式!

为了方便举例,我以GitHub和Gitee(码云)作为示例!

同步方式

命令方式同步

git remote rm origin

然后,先关联GitHub的远程库:

git remote add github [email protected]:chloneda/demo.git

接着,再关联码云的远程库:

git remote add gitee [email protected]:chloneda/demo.git

配置方式同步

修改.git文件夹内的config文件

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = [email protected]:chloneda/demo.git
    fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

将上述文件内容[remote "origin"]内容复制,修改origin名称,内容如下。

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "github"]
    url = [email protected]:chloneda/demo.git
    fetch = +refs/heads/*:refs/remotes/github/*
[remote "gitee"]
    url = [email protected]:chloneda/demo.git
    fetch = +refs/heads/*:refs/remotes/gitee/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

查看远程库

通过以上两种方式的任一种方式配置完成后,我们用git remote -v查看远程库信息:

gitee   [email protected]:chloneda/demo.git (fetch)
gitee   [email protected]:chloneda/demo.git (push)
github  [email protected]:chloneda/demo.git (fetch)
github  [email protected]:chloneda/demo.git (push)

可以看到两个远程库,说明配置生效了

上传代码

git add .
git commit -m "update"

提交到github

git push github master

提交到码云

git push gitee master

更新代码

# 从github拉取更新
git pull github

# 从gitee拉取更新
git pull gitee

猜你喜欢

转载自www.cnblogs.com/chloneda/p/git-to-github-gitee.html