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

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

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


当前回答

如果目的地是空的,这是我发现的最简洁的方法。切换到一个空文件夹,然后:

# Note the period for cwd >>>>>>>>>>>>>>>>>>>>>>>> v
git clone --bare https://your-source-repo/repo.git .
git push --mirror https://your-destination-repo/repo.git

用https://..。对于文件:///your/repo等。

其他回答

这里有另一个对同一件事的看法,它更适合我所处的情况。它解决了您有多个远程的问题,想要克隆远程源中的所有分支到远程目标,但不需要事先检查它们。

(我与丹尼尔的解决方案的问题是,它将拒绝从源远程签出跟踪分支,如果我之前已经签出了它,即,它不会在推送之前更新我的本地分支)

git push destination +refs/remotes/source/*:refs/heads/*

注意: 如果你不直接使用CLI,你必须转义星号: Git push destination +refs/remotes/source/\*:refs/heads/\* @mattalxndr

这将把远程源中的所有分支推到目标中的头分支,可能执行非快进推。你仍然需要分别推送标签。

要推送分支和标签(但不包括遥控器):

git push origin 'refs/tags/*' 'refs/heads/*'

这相当于结合了git push的——tags和——all选项,而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

实现目标的主要方法是使用——all和——tags标记。忽略其中任何一个都不能实现你想要的东西。不幸的是,它们不能一起使用(不知道为什么),所以它们必须一个接一个地运行。

git push --all
git push --tags

另一个相关的选项是——prune选项,它删除远程上本地不存在的任何分支/标签。

或者,考虑——mirror选项,因为它基本上等同于——all——tags——prune。

git push --mirror