Reputation: 14818
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?
Upvotes: 831
Views: 405936
Reputation: 7469
To push all your branches, you can use:
git push --all
However, this does not set upstreams for the pushed branches. In order to fix this, use instead:
git push -u -all
If you already pushed your branches without the -u
switch, you can run the following to automate setting upstream for all local branches:
for branch in $(git branch --format='%(refname:short)' | grep -v master); do
git branch --set-upstream-to=origin/$branch $branch;
done
Note the pipe to grep with inverse match switch ( | grep -v master
).
This allows to skip the master branch which presumably is already tracking the correct remote branch.
You can remove that part if it is not the case.
Upvotes: 0
Reputation: 1311
I have done a little update on @Frizz1977 answer
git clone --mirror OLD_GIT_URL
cd NEW_CREATED_FOLDER
git remote set-url NEW-REMOTE NEW_GIT_URL
git push NEW-REMOTE --mirror
the most common NEW-REMOTE name is "origin"
Upvotes: 0
Reputation: 286
To push all branches on your remote you just need to commit all branches first and then try below command
git push --all your-remote-branch-name
Like
git push --all origin(my remote name)
Hope it will help(:
Upvotes: 5
Reputation: 1061
Run following to move existing repository to new remote with all branches and tags:
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
Upvotes: 16
Reputation: 41
Here is What I solved [remote rejected] when I push local Git repo to new remote including all branches and tags
git clone --mirror old-repo
cd <name-repo.git>
git remote add new new-repo
git push new --mirror
Upvotes: 3
Reputation: 7224
The main way to do what you want is to use the --all
and --tags
flags. Leaving out either will not push part of what you want. Unfortunately, they cannot be used together (don't see why) so they must be run one after the other.
git push --all
git push --tags
Another option that is relevant is the --prune
option that removes any branches/tags on the remote that don't exist locally.
Alternatively, consider the --mirror
option as it is basically equivalent to --all --tags --prune
.
git push --mirror
Upvotes: 2
Reputation: 387
I found the best and simplest method https://www.metaltoad.com/blog/git-push-all-branches-new-remote, works like a charm for me, it will push all the tags and branches from origin to the new remote:
git remote add newremote new-remote-url
git push newremote --tags refs/remotes/origin/*:refs/heads/*
I used 'git push --all -u newremote', but it only push the checkouted branches to the 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
Upvotes: 6
Reputation: 890
Every time I Google how to do this I end up reading this same thread, but it doesn't get me where I need to be, so hopefully this will help my future self and others too.
I started a new local project that I want to push to my repo (BitBucket). Here is what I did:
git init
git add .
git commit -m "Initial commit"
new_project
git remote add origin [email protected]:AndrewFox/new_project.git
git push origin master -f
The -f
flag is to force the push, otherwise it will identify that the two repo's are different and fail.
Upvotes: 0
Reputation: 1167
My favorite (and simplest) way
git clone --mirror OLD_GIT_URL
cd NEW_CREATED_FOLDER
git remote add NEW-REMOTE NEW_GIT_URL
git push NEW-REMOTE --mirror
Upvotes: 17
Reputation: 999
I was in a process of switching from one version control service to another and needed to clone all repositories including all branches, tags and history.
To achieve above I did next:
git push origin '*:*'
.sh script used to checkout all branches to local repository:
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
git branch --track ${branch#remotes/origin/} $branch
done
Upvotes: 7
Reputation: 2443
Below command will push all the branches(including the ones which you have never checked-out but present in your git repo, you can see them by git branch -a
)
git push origin '*:*'
NOTE: This command comes handy when you are migrating version control service(i.e migrating from Gitlab to GitHub)
Upvotes: 3
Reputation: 3368
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.
cd ..
rm -rf old-repository.git
Mirroring a repository that contains Git Large File Storage objects
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 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 lfs push --all https://github.com/exampleuser/new-repository.git
Remove the temporary local repository you created in step 1.
cd ..
rm -rf old-repository.git
Above instruction comes from Github Help: https://help.github.com/articles/duplicating-a-repository/
Upvotes: 40
Reputation: 117008
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.
Upvotes: 1308
Reputation: 2974
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):
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
Upvotes: 1
Reputation: 5689
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.
Upvotes: 116
Reputation: 869
Based in @Daniel answer I did:
for remote in \`git branch | grep -v master\`
do
git push -u origin $remote
done
Upvotes: 3
Reputation: 27214
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.
Upvotes: 30
Reputation: 963
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 --bare
and --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.
Upvotes: 8
Reputation: 878
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.
Upvotes: 5
Reputation: 7281
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".
Upvotes: 16
Reputation: 23359
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
Upvotes: 240