背景
之前需要从外部gitlab同步代码到公司服务器上, 不想一个分支一个分支检出代码, 于是搜索了下git命令的实现方式, 在Stack Overflow 发现了Set up git to pull and push all branches这个问题
实现思路
-
通过refs引用方式推送并排除HEAD分支 (推荐方式) git clone <url_of_old_repo>
cd <name_of_old_repo>
git remote add new-origin <url_of_new_repo>
git ls-remote . | grep 'refs/remotes/origin/' | grep -v 'HEAD' | awk -F 'origin/' '{print $2}' | xargs -i git push -f new-origin --tags refs/remotes/origin/{}:refs/heads/{}
命令解释: git ls-remote . 会打印出origin 即原有远端仓库下的所有分支 例如 [aaa@bbb proj.git]$ git ls-remote .
3a14dbb3d42461e7596702ceefd0c3b86d187d76 HEAD
3a14dbb3d42461e7596702ceefd0c3b86d187d76 refs/heads/master
3a14dbb3d42461e7596702ceefd0c3b86d187d76 refs/remotes/origin/HEAD
3a14dbb3d42461e7596702ceefd0c3b86d187d76 refs/remotes/origin/master
cc53d8bd2e66218aefcfb1470e83370110a255c1 refs/remotes/origin/v0.6.0
通过grep 'refs/remotes/origin/' | grep -v 'HEAD' | awk -F 'origin/' '{print $2}' 命令就能获得第二列除HEAD 外的所有远端分支名
参考
Set up git to pull and push all branches How to delete HEAD branch on remote Git repository?
|