• 如果是在 gitee.com 和 github.com 上同步代码,假设默认仓库放到 github.com,只需要 git remote add 仓库别名 gitee.com上的仓库地址,然后在代码有提交时,执行 git push 仓库别名 即可达到目的,仓库别名 自己起一个好记的就行,比如 gitee
  • 现在假设在办公室只能用局域网的 git server,到了周末,放不下工作,想在家继续 coding,那么到了下周一,如何将代码同步到办公室的 git server 呢?

模拟这个场景

  • 假设在家里能访问 coding.net,但不能访问 gitee.com(模拟办公室的局域网环境)。
  • 而在办公室,即可访问 coding.net 也可访问 gitee.com。
  • 正在进行的项目为:remoteT

在办公室, 周五下班前

  1. 同时在 gitee.com 和 coding.net 上创建同名仓库(项目):remoteT,然后将 gitee.com 上的仓库 remoteT clone 到本地。

    1
    git clone https://gitee.com/uncleAndyChen/remoteT.git
  2. 添加文件 readme.md,写一行文本:1. from gitee

  3. 推送

    1
    2
    3
    git add .
    git commit -m'from gitee'
    git push
  4. 添加 coding.net 上的仓库 remoteT ,作为远程仓库来管理,添加后将本地代码推送,使用 Git Bash 或者 cmd,在项目根目录下执行如下指令:

    1
    2
    git remote add coding https://e.coding.net/andychen/remoteT.git
    git push coding

至此,两个 git server 上的仓库 remoteT 的代码就完全一样了。

远程仓库的配置信息保存到 .git 目录下的 config 文件中了,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://gitee.com/uncleAndyChen/remoteT.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "coding"]
url = https://e.coding.net/andychen/remoteT.git
fetch = +refs/heads/*:refs/remotes/coding/*

如果要修改远程仓库的别名,把 [remote "coding"]fetch = +refs/heads/*:refs/remotes/coding/* 中的 coding 改为想的名字即可。

周末,在家里

  1. 获取 coding.net 上的仓库 remoteT

    1
    git clone https://e.coding.net/andychen/remoteT.git
  2. 修改 readme.md 文件,追加一行文本 2. from coding.net,修改好之后推送。

到了下周一,回到办公室

  1. 获取在 coding.net 上的仓库 remoteT 的修改,获取时需要指定分支:git pull coding master,这里的 coding 只是远程仓库的别名,创建的时候起的名字(通过命令 git remote add coding https://e.coding.net/andychen/remoteT.git 创建)。

    1
    2
    3
    4
    5
    6
    7
    $ git pull coding master
    From https://e.coding.net/andychen/remoteT
    * branch master -> FETCH_HEAD
    Updating 8c02788..ab9d5a4
    Fast-forward
    readme.md | 3 ++-
    1 file changed, 2 insertions(+), 1 deletion(-)
  2. 把在 coding.net 上的仓库 remoteT 的修改再 push 到 gitee.com 上的仓库 remoteT,执行 git push 就行,因为 gitee.com 上的仓库 remoteT 是默认仓库。

    1
    2
    3
    4
    5
    6
    7
    8
    $ git push
    Enumerating objects: 5, done.
    Counting objects: 100% (5/5), done.
    Writing objects: 100% (3/3), 285 bytes | 95.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    remote: Powered By Gitee.com
    To https://gitee.com/uncleAndyChen/remoteT.git
    8c02788..ab9d5a4 master -> master

至此,周末在家里通过 coding.net 上的仓库 remoteT 写的代码就完全同步到 gitee.com 上的仓库 remoteT 了。文件 readme.md 的内容变成了:

1
2
1. from gitee
2. from coding.net

获取远程仓库 coding(通过命令 git remote add coding https://e.coding.net/andychen/remoteT.git 创建)的代码时,需要指定分支,否则:You asked to pull from the remote ‘coding’, but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line.

1
2
3
4
5
6
7
8
9
10
$ git pull coding
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://e.coding.net/andychen/remoteT
8c02788..ab9d5a4 master -> coding/master
You asked to pull from the remote 'coding', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

注意事项

在实际推送的时候,如果提示:Updates were rejected because the remote contains work that you do … This is usually caused by another repository pushing

通常意味着离上次 git pull 之后,已经有了新的 push,要先 git pull,再 git push

1
2
3
4
5
6
7
8
9
[utomcat@localhost mis-api]$ git push gitee
To gitee.com:uncleAndyChen/mis-api.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@gitee.com:uncleAndyChen/mis-api.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.