使用 git subtree
拆分 git仓库
拆分仓库
git
├── module-A
├── sub-module1
├── .git
└── sub-module2
需要将 module-A
保留提交记录拆分出来,流程如下:
cd git/module-A
git subtree split -P module-A -b module-A-only (将module-A文件夹拆分为分支module-A-only)
cd -
mkdir moduleA
git init
git checkout -b dev (创建分支dev)
git pull ../git/module-A module-A-only (将module-A拆分分支拉到本地仓库)
git remote add origin https://xxx.xxx.xxx.xxx/git/module-A.git (已经创建好远程git仓库 module-A)
git pull origin dev --allow-unrelated-histories (如果远程仓库不是裸仓库,需要执行这一步,gerrit创建project会产生提交)
git push origin -u dev (将本地分支推送到远程dev分支上)
如果要迁移所有的分支信息,需要重复上面步骤中对dev
分支的操作
可以将git/module-A
下的资料清除掉
git filter-branch -f --index-filter "git rm -r -f -q --cached --ignore-unmatch module-A" --prune-empty HEAD
迁移仓库
迁移仓库可以保留原仓库的所有信息,包括提交历史及分支,需要使用裸仓库
如果你想从别的 Git 托管服务那里复制一份源代码到新的 Git 托管服务器上的话,可以通过以下步骤来操作。
从原地址克隆一份裸版本库,比如原本托管于 GitHub。
git clone --bare git://github.com/username/project.git
然后到新的 Git 服务器上创建一个新项目,比如 GitCafe。
以镜像推送的方式上传代码到 GitCafe 服务器上。
cd project.git
git push --mirror git@gitcafe.com/username/newproject.git
删除本地代码
cd ..
rm -rf project.git
到新服务器 GitCafe 上找到 Clone 地址,直接 Clone 到本地就可以了。
git clone git@gitcafe.com/username/newproject.git
gerrit
通过 gerrit
创建的 project
会有权限问题,导致拆分仓库时最后一步失败
Pushing to https://gerrit.googlesource.com/gerrit
POST git-receive-pack (11557 bytes)
remote: Resolving deltas: 100% (76/76) remote: Resolving deltas: 100% (76/76)
remote: Processing changes: refs: 1 remote: Processing changes: refs: 1, done
remote:
remote: ERROR: In commit 0a99723fd7039844ce697997916910ce11bdcb4a
remote: ERROR: committer email address mani_c...@yahoo.co.in
remote: ERROR: does not match your user account.
remote: ERROR:
remote: ERROR: The following addresses are currently registered:
remote: ERROR: mani.c...@tcs.com
remote: ERROR:
remote: ERROR: To register an email address, please visit:
remote: ERROR: https://gerrit-review.googlesource.com/#/settings/contact
remote:
remote:
To https://gerrit.googlesource.com/gerrit
两种原因:
- incorrect configuration of the e-mail address on client or server side
- missing privileges to push commits that were committed by other users
应该是第二个原因导致的,解释如下:
If pushing to Gerrit fails with the error message “invalid committer” and somebody else committed the change for which the push fails, then you have no permissions to forge the committer identity. In this case you may contact the project owner to request the ‘Forge Committer’ access right or ask the maintainer to commit this change on the author’s behalf.
在 gerrit
中添加 Forge Committer
权限解决此问题