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

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

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


当前回答

要推送所有分支,可以使用以下任意一种(将REMOTE替换为远程的名称,例如“origin”):

git push REMOTE '*:*'
git push REMOTE --all

推送所有标签:

git push REMOTE --tags

最后,我认为你可以在一个命令中完成这一切:

git push REMOTE --mirror

然而,除此之外——镜子,也会推动你的遥控器,所以这可能不是你想要的。

其他回答

我发现这些方法似乎都不适合我。放心燃烧这个死亡,但由于某种原因不能让其他选项正常工作。

预期的结果是回购“克隆”到另一个远程(即从Github到另一个提供商):

所有分支都在新的远程上创建 所有分支历史记录都在新的远程上创建 (我尝试的每个解决方案都错过了这一点) 所有标记都在新的远程上创建 源移动(给定) 非破坏性(暂停——mirror选项)

我看到的主要问题是,所有远程分支都没有在新的远程中重新创建。如果一个命令有,新的远程没有分支历史记录(即做一个git签出分支;Git日志不会显示预期的分支提交)。

我注意到git checkout -b branchname与git checkout branchname不相同(后者是我需要的)。我注意到git checkout -track branchname似乎没有拉出分支历史。

我的解决方案(基于powershell):

Function Git-FetchRemoteBranches {
$originalbranch = (git symbolic-ref HEAD).split("/")[-1]

Foreach ($entry in (git branch -r)) {

If ($entry -like "*->*") {
  $branch = $entry.split("->")[2].split("/")[1]
}
  else {$branch = $entry.split("/")[1]}

Write-Host "--Trying git checkout " -NoNewline
Write-Host "$branch" -Foreground Yellow

git checkout $branch

Remove-Variable branch -Force

""}

#Switch back to original branch, if needed
If ( ((git symbolic-ref HEAD).split("/")[-1]) -ne $originalbranch) {
"Switching back to original branch"
git checkout $originalbranch
Remove-Variable originalbranch -Force
}
}

git clone http://remoterepo
cd remoterepo
Git-FetchRemoteBranches
git remote add newremote
git push newremote --all
git push newremote --tags #Not sure if neeeded, but added for good measure

我找到了最好最简单的方法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

对我来说,有效的方法是。

git push origin --all

以下是我解决[远程拒绝]时,我将本地Git回购到新的远程,包括所有分支和标签

Git克隆——镜像old-repo cd < name-repo.git > Git远程添加新的new-repo Git push new——mirror

在像我这样的情况下,你获得了一个回购,现在将远程源切换到一个不同的回购,一个新的空的…

你在里面有repo和所有分支,但你仍然需要为git push -all命令签出这些分支。

你应该在推之前这样做:

for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done

紧随其后的是

git push --all