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

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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. It looks like I basically just need to do a git push, but that only uploads the master branch. How do I push everything so I get a full replica of my local repo on the remote?

回答1:

To push all your branches, use either (replace REMOTE with the name of the remote, for example "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.



回答2:

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 --all command to actually push those too.

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


回答3:

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 source to remote destination but without having to check them all out beforehand.

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

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/\*

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



回答4:

The manpage for git-push is worth a read. Combined with this website I wrote the following in my .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".

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

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".



回答5:

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/repo etc. as appropriate.



回答6:

In my case what worked was.

git push origin --all


回答7:

To push branches and tags (but not remotes):

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

This would be equivalent to combining the --tags and --all options for git push, which git does not seem to allow.



回答8:

I found above answers still have some unclear things, which will mislead users. First, It's sure that git push new_origin --all and git push new_origin --mirror can't duplicate all branches of origin, it just duplicate your local existed branches to your 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 --mirror will download a bare repo,not including workspace. please refer this

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

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



回答9:

Based in @Daniel answer I did:

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


回答10:

I found that none of these seemed to work properly for me. Feel free to flame this to death but for some reason couldn't get the other options to work properly.

Expected result was a repo "cloned" to another remote (ie from Github to another provider):

  • All branches are created on new remote
  • All branch history are created on new remote
    • (this was missed on every solution I tried)
  • All tags are created on new remote
  • Source moves over (a given)
  • Non-destructive (giving pause to the --mirror option)

The major issue I was seeing was either all remote branches didn't get recreated in the new remote. If a command did, the new remote did not have the branch history (ie doing a git checkout branch; git log wouldn't show the expected branch commits).

I noticed git checkout -b branchname is NOT the same as git checkout branchname (the latter being what I needed). I notice git checkout --track branchname didn't appear to pull the branch history.

My Solution (powershell based):

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!