Loading

git 仓库迁移,保留提交记录,同时提交到多个仓库

仓库迁移并保留原有的提交记录

应用场景

原有需求在 A 存储库地址进行开发,现在新建了一个 B 存储库,要求将 A 的代码和提交记录一起转移到 B

解决方案

1. git clone A // 拉取A仓库的代码到本地
2. cd AA(文件夹名称) // 进入代码仓库
3. git branch -r // 查看远程存储库的根节点名称(一般都是origin)
4. git remote set-url origin B // 将本地的远程仓库的地址设置为B
5. git push --mirror // 将已经修改远程仓库地址的代码,以镜像的方式推到远程B
6. done 此时原有的A本地的代码仓库已经变成了B的本地代码仓库,以后提交会直接提交至B

注意

如果想把所有的分支和记录都转移到新仓库,需要本地先把不同分支的代码checkout出来,以下提供一种批量的做法

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

将本地代码的修改同时提交到多个仓库

应用场景

在前一个场景的基础上,有可能需要保留原有的仓库(作为备用仓库),本地在使用 git push 时,要求同时提交到两个仓库

解决方案

打开 .git/config 文件,在原有的 url 下面添加需要推送的仓库地址

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "origin"]
	url = A仓库地址
	url = B仓库地址(新增)
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[branch "xxx分支"]
	remote = origin
	merge = refs/heads/xxx分支
posted @ 2020-11-03 19:17  Frank-Link  阅读(2338)  评论(0编辑  收藏  举报