我有一个本地Git回购,我想把它推到一个新的远程回购(全新的回购设置在Beanstalk上,如果这很重要的话)。 我的本地回购有一些分支和标签,我想保留我的所有历史。

看起来我基本上只需要做一个git推送,但这只上传主分支。

我如何推动所有东西,以便在遥控器上获得本地回购的完整副本?


当前回答

我找到了最好最简单的方法https://www.metaltoad.com/blog/git-push-all-branches-new-remote,对我来说就像一个魅力,它会把所有的标签和分支从原点推到新的遥控器:

git remote add newremote new-remote-url

git push newremote --tags refs/remotes/origin/*:refs/heads/*

我使用'git push——all -u newremote',但它只会将检出的分支推到newremote。

Git: Push All Branches to a New Remote by Keith Dechant , Software Architect Here's a scenario some of you might have encountered with your Git repositories. You have a working copy of a Git repo, say from an old server. But you only have the working copy, and the origin is not accessible. So you can't just fork it. But you want to push the whole repo and all the branch history to your new remote. This is possible if your working copy contains the tracking branches from the old remote (origin/branch1, origin/branch1, etc.). If you do, you have the entire repo and history. However, in my case there were dozens of branches, and some or all of them I had never checked out locally. Pushing them all seemed like a heavy lift. So, how to proceed? I identified two options: Option 1: Checkout every branch and push I could do this, and I could even write a Bash script to help. However, doing this would change my working files with each checkout, and would create a local branch for each of the remote tracking branches. This would be slow with a large repo. Option 2: Push without changing your working copy There is a second alternative, which doesn't require a checkout of each branch, doesn't create extraneous branches in the working copy, and doesn't even modify the files in the working copy. If your old, no-longer-active remote is called "oldremote" and your new remote is called "newremote", you can push just the remote tracking branches with this command: git push newremote refs/remotes/oldremote/*:refs/heads/* In some cases, it's also possible to push just a subset of the branches. If the branch names are namespaced with a slash (e.g., oldremote/features/branch3, oldremote/features/branch4, etc.), you can push only the remote tracking branches with names beginning with "oldremote/features": git push newremote refs/remotes/oldremote/features/*:refs/heads/features/* Whether you push all the branches or just some of them, Git will perform the entire operation without creating any new local branches, and without making changes to your working files. Every tracking branch that matches your pattern will be pushed to the new remote. For more information on the topic, check out this thread on Stack Overflow. Date posted: October 9, 2017

其他回答

根据@Daniel的回答,我做了:

for remote in \`git branch | grep -v master\`
do 
    git push -u origin $remote
done

我正处于从一个版本控制服务切换到另一个版本控制服务的过程中,需要克隆所有存储库,包括所有分支、标签和历史记录。

为了实现以上目标,我做了下面的事情:

手动签出所有分支到本地存储库(脚本签出所有如下所示), Git push origin '*:*'

.sh脚本用于检出所有分支到本地存储库:

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch
done

我发现上面的答案还有一些不清楚的地方,会误导用户。首先,git push new_origin——all和git push new_origin——mirror不能复制所有的origin分支,它只能复制你本地已经存在的分支到你的new_origin。

下面是我测试过的两种有用的方法:

1、副本由克隆裸回购。Git clone——bare origin_url,然后进入文件夹,然后Git push new_origin_url——mirror。通过这种方式,你也可以使用git clone——mirror origin_url, both——bareand——mirror将下载一个裸回购,不包括工作区。请参考这个

2、如果你有一个使用git克隆的git repo,这意味着你有裸repo和git工作区,你可以使用git远程添加new_origin new_origin_url,然后git push new_origin +refs/remotes/origin/\*:refs/heads/\*,然后git push new_origin——tags

通过这种方式,你会得到一个额外的头部分支,这是没有意义的。

镜像存储库

创建存储库的裸克隆。

git clone --bare https://github.com/exampleuser/old-repository.git

镜像推送到新的存储库。

cd old-repository.git
git push --mirror https://github.com/exampleuser/new-repository.git

删除在步骤1中创建的临时本地存储库。

cd ..
rm -rf old-repository.git

镜像包含Git大文件存储对象的存储库

创建存储库的裸克隆。将示例用户名替换为拥有存储库的个人或组织的名称,并将示例存储库名称替换为希望复制的存储库的名称。

git clone --bare https://github.com/exampleuser/old-repository.git

导航到刚才克隆的存储库。

cd old-repository.git

导入存储库的Git大文件存储对象。

git lfs fetch --all

镜像推送到新的存储库。

git push --mirror https://github.com/exampleuser/new-repository.git

将存储库的Git大文件存储对象推到镜像。

git lfs push --all https://github.com/exampleuser/new-repository.git

删除在步骤1中创建的临时本地存储库。

cd ..
rm -rf old-repository.git

以上说明来自Github帮助:https://help.github.com/articles/duplicating-a-repository/

我最喜欢的(也是最简单的)方法

git clone --mirror  OLD_GIT_URL
cd  NEW_CREATED_FOLDER
git remote add NEW-REMOTE NEW_GIT_URL
git push  NEW-REMOTE --mirror