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

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

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


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

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

推送所有标签:

git push REMOTE --tags

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

git push REMOTE --mirror

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


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

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

你应该在推之前这样做:

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

紧随其后的是

git push --all

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

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

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

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

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


git-push的手册值得一读。结合这个网站,我在我的。git/config中写了以下内容:

[remote "origin"]
    url = …
    fetch = …
    push = :
    push = refs/tags/*

push =:表示“推送任何‘匹配的’分支(即已经存在于远程存储库并且有本地对应的分支)”,而push = refs/tags/*表示“推送所有标签”。

所以现在我只需要运行git push来推送所有匹配的分支和所有标签。

是的,这不是OP想要的(所有要推送的分支必须在远程端已经存在),但是对于那些在谷歌搜索“如何同时推送分支和标签”时发现这个问题的人来说可能是有帮助的。


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

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

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

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

这相当于结合了git push的——tags和——all选项,而git似乎不允许这样做。


我发现上面的答案还有一些不清楚的地方,会误导用户。首先,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 push origin --all

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

# 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等。


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

预期的结果是回购“克隆”到另一个远程(即从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

镜像存储库

创建存储库的裸克隆。

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 repo中出现的分支,你可以通过git branch -a看到它们)

Git push origin '*:*'

注意:当您正在迁移版本控制时,此命令非常方便 (我的服务。e从Gitlab迁移到GitHub)


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

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

手动签出所有分支到本地存储库(脚本签出所有如下所示), 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 clone --mirror  OLD_GIT_URL
cd  NEW_CREATED_FOLDER
git remote add NEW-REMOTE NEW_GIT_URL
git push  NEW-REMOTE --mirror 

每次我谷歌如何做到这一点,我最终都会读到同样的帖子,但它并没有让我到达我需要的地方,所以希望这对我未来的自己和其他人也有帮助。

我开始了一个新的本地项目,我想把它推到我的回购(比特桶)。以下是我所做的:

导航到本地项目根目录 初始化:git init 使用:git Add添加所有文件。 git commit -m“初始提交” 去我的回购(BitBucket) 创建新的存储库:new_project 回到我的本地项目 add origin git@bitbucket.org:AndrewFox/new_project.git 使用:git Push origin master -f来提交

-f标志用于强制执行,否则它将识别出两个回购是不同的并失败。


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


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

git push --all
git push --tags

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

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

git push --mirror

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

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


运行以下命令将现有的存储库移动到带有所有分支和标签的新远程:

cd existing_repo
git remote rename origin old-origin
git remote add origin git@<repo-url.git>
for remote in `git branch -r `; do git checkout --track remotes/$remote ; done
git push -u origin --all
git push -u origin --tags