- 场景一,迁移仓库(含提交历史记录)
- 场景二,从旧库同步代码
场景一,迁移仓库(含提交历史记录)
- 需要从第一个仓库
repo-old
转移到第二个仓库repo-current
,包括提交历史记录,同时两个仓库在不同的用户下面。 执行
git remote add repo-current https://gitee.com/uncleAndyChen/repo-current.git
,添加第二个仓库为远端仓库,这时,.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/elsafly/repo-old.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "repo-current"]
url = https://gitee.com/uncleAndyChen/repo-current.git
fetch = +refs/heads/*:refs/remotes/gitee/*执行
git push repo-current
之后,本地获取到的所有提交都会 push 到远端仓库repo-current
。修改 git 配置
.git/config
为如下内容:1
2
3
4
5
6
7
8
9
10
11
12
13[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://gitee.com/uncleAndyChen/repo-current.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master配置修改完,需要在控制面板的
Control Panel\User Accounts\Credential Manager -> Windows Credentials
下删除 gitee 的认证信息- 再次执行 git 更新(pull 和 push)时会提示输入用户认证信息(登录)。
- 重新论证之后,就跟使用之前的仓库没什么区别了(提交历史都在)。现在,迁移仓库就算完成了。
场景二,从旧库同步代码
在场景一的情况下,如果在操作 git push repo-current
时,还有一次或多次 commit 没有 pull,那么,接下来的操作,就是将这些 commit 同步到新的仓库。
执行
git remote add repo-old https://gitee.com/elsafly/repo-old.git
,把旧仓库加为远端仓库。这时,配置内容如下: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/repo-current.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "repo-old"]
url = https://gitee.com/elsafly/repo-old.git
fetch = +refs/heads/*:refs/remotes/gitee/*pull 原来的仓库,会报错:
1
2
3
4git pull repo-old"
...
You asked to pull from the remote 'elsafly', 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.解决:将
[branch "master"]
节点的remote
指向改为需要 pull 的远端仓库名,即改为repo-old
,修改之后,内容如下: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/repo-current.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = repo-old
merge = refs/heads/master
[remote "repo-old"]
url = https://gitee.com/elsafly/repo-old.git
fetch = +refs/heads/*:refs/remotes/gitee/*然后执行
git pull
- 如果本地有修改,则会提示 merge,如果有冲突,还需要手动解决冲突。
- 然后,恢复配置,继续用 origin 的远端仓库。将
remote = repo-old
改回remote = origin
- 执行
git push
,达到同步目的。
注意事项
- 在都是 gitee 的情况下,要使用不同用户下的仓库,在切换仓库地址之后,需要在控制面板的
Control Panel\User Accounts\Credential Manager -> Windows Credentials
下删除认证信息,然后再执行 git 更新(pull 和 push),执行更新时会提示输入用户认证信息(登录)。 - 否则会报错:
1
2
3git pull repo-old
remote: You do not have permission to pull from the repository via HTTPS
fatal: Authentication failed for 'https://gitee.com/elsafly/repo-old.git/'