将本地 Git 存储库推送到新的远程,包括所有分支和标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6865302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:25:59  来源:igfitidea点击:

Push local Git repo to new remote including all branches and tags

git

提问by Cory Imdieke

I have a local Git repo that I would like to push to a new remote repo (brand new repo set up on Beanstalk, if that matters).
My local repo has a few branches and tags, and I would like to keep all of my history.

我有一个本地 Git 存储库,我想将它推送到一个新的远程存储库(如果重要的话,在 Beanstalk 上设置的全新存储库)。
我的本地仓库有一些分支和标签,我想保留我所有的历史记录。

It looks like I basically just need to do a git push, but that only uploads the masterbranch.

看起来我基本上只需要做一个git push,但这只会上传master分支。

How do I push everything so I get a full replica of my local repo on the remote?

如何推送所有内容,以便在远程获得本地存储库的完整副本?

回答by cmcginty

To push all your branches, use either (replace REMOTE with the name of the remote, for example "origin"):

要推送所有分支,请使用任一方法(将 REMOTE 替换为远程名称,例如“origin”):

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

To push all your tags:

推送所有标签

git push REMOTE --tags

Finally, I think you can do this all in one command with:

最后,我认为您可以使用以下命令在一个命令中完成所有操作:

git push REMOTE --mirror

However, in addition --mirror, will also push your remotes, so this might not be exactly what you want.

但是,此外--mirror,还会推送您的遥控器,因此这可能不是您想要的。

回答by Daniel

In the case like me that you aquired a repo and are now switching the remote origin to a different repo, a new empty one...

在像我这样的情况下,您获得了一个仓库,现在正在将远程源切换到另一个仓库,一个新的空仓库......

So you have your repo and all the branches inside, but you still need to checkout those branches for the git push --allcommand to actually push those too.

所以你有你的 repo 和里面的所有分支,但你仍然需要检查这些分支,git push --all命令才能实际推送这些分支。

You should do this before you push:

您应该在推送之前执行此操作:

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

Followed by

其次是

git push --all

回答by Pieter Breed

Here is another take on the same thing which worked better for the situation I was in. It solves the problem where you have more than one remote, would like to clone all branches in remote sourceto remote destinationbut without having to check them all out beforehand.

这是对同一件事的另一种看法,它在我所处的情况下效果更好。它解决了您有多个遥控器的问题,想将远程的所有分支克隆source到远程,destination但无需事先检查它们。

(The problem I had with Daniel's solution was that it would refuse to checkout a tracking branch from the sourceremote if I had previously checked it out already, ie, it would not update my local branch before the push)

(我在 Daniel 的解决方案中遇到的问题是,source如果我之前已经检出过跟踪分支,它将拒绝从远程检出跟踪分支,即,它不会在推送之前更新我的本地分支)

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

Note: If you are not using direct CLI, you must escape the asterisks:

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

注意:如果您不使用直接 CLI,则必须避开星号:

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

this will push all branches in remote sourceto a head branch in destination, possibly doing a non-fast-forward push. You still have to push tags separately.

这会将 remotesource中的所有分支推送到 中的 head 分支destination,可能会进行非快进推送。您仍然需要单独推送标签。

回答by ta.speot.is

This is the most concise way I have found, provided the destination is empty. Switch to an empty folder and then:

这是我找到的最简洁的方法,前提是目的地是空的。切换到一个空文件夹,然后:

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

Substitute https://...for file:///your/repoetc. as appropriate.

替换https://...file:///your/repo等中的。

回答by scy

The manpage for git-pushis worth a read. Combined with this websiteI wrote the following in my .git/config:

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

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

The push = :means "push any 'matching' branches (i.e. branches that already exist in the remote repository and have a local counterpart)", while push = refs/tags/*means "push all tags".

push = :手段“推动任何‘匹配’分支(即已经存在于远程仓库,并有当地合作即分行)”,而push = refs/tags/*表示“推动所有标签”。

So now I only have to run git pushto push all matching branches and all tags.

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

Yes, this is not quite what the OP wanted (all of the branches to push must already exist on the remote side), but might be helpful for those who find this question while googling for "how do I push branches and tags at the same time".

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

回答by nomulex

In my case what worked was.

就我而言,有效的是。

git push origin --all

回答by Micha? Zalewski

Mirroring a repository

镜像仓库

Create a bare clone of the repository.

创建存储库的裸克隆。

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

Mirror-push to the new repository.

镜像推送到新的存储库。

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

Remove the temporary local repository you created in step 1.

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

cd ..
rm -rf old-repository.git

Mirroring a repository that contains Git Large File Storage objects

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

Create a bare clone of the repository. Replace the example username with the name of the person or organization who owns the repository, and replace the example repository name with the name of the repository you'd like to duplicate.

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

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

Navigate to the repository you just cloned.

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

cd old-repository.git

Pull in the repository's Git Large File Storage objects.

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

git lfs fetch --all

Mirror-push to the new repository.

镜像推送到新的存储库。

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

Push the repository's Git Large File Storage objects to your mirror.

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

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

Remove the temporary local repository you created in step 1.

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

cd ..
rm -rf old-repository.git

Above instruction comes from Github Help: https://help.github.com/articles/duplicating-a-repository/

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

回答by yanzi1225627

I found above answers still have some unclear things, which will mislead users. First, It's sure that git push new_origin --alland git push new_origin --mirrorcan't duplicate all branches of origin, it just duplicate your local existed branches to your new_origin.

我发现上面的回答还有一些不清楚的地方,会误导用户。首先,可以肯定的是git push new_origin --allgit push new_origin --mirror不能复制所有的原始分支,它只是将您本地现有的分支复制到您的 new_origin。

Below is two useful methods I have tested:

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

1,duplicate by clone bare repo.git clone --bare origin_url, then enter the folder, and git push new_origin_url --mirror.By this way, you can also use git clone --mirror origin_url, both --bareand --mirrorwill download a bare repo,not including workspace. please refer this

1,通过克隆裸回购复制。git clone --bare origin_url,然后进入该文件夹,git push new_origin_url --mirror。通过这种方式,你也可以使用git clone --mirror origin_url,无论是--bare--mirror将下载的裸回购,不包括工作区。请参考这个

2,If you have a git repo by using git clone, which means you have bare repo and git workspace, you can use git remote add new_origin new_origin_url, and then git push new_origin +refs/remotes/origin/\*:refs/heads/\*,and then git push new_origin --tags

2、如果你通过 using 有一个 git repo git clone,这意味着你有裸 repo 和 git 工作区,你可以使用git remote add new_origin new_origin_url, 然后git push new_origin +refs/remotes/origin/\*:refs/heads/\*, 然后git push new_origin --tags

By this way, you will get a extra head branch, which make no sense.

通过这种方式,您将获得一个额外的头部分支,这是没有意义的。

回答by Arthur Juli?o

Based in @Danielanswer I did:

基于@Daniel 的回答,我做了:

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

回答by CEL

To push branches and tags (but not remotes):

推送分支和标签(但不是遥控器):

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

This would be equivalent to combining the --tagsand --alloptions for git push, which git does not seem to allow.

这相当于结合了--tags--all选项git push,这似乎是 git 不允许的。