Set up git to pull and push all branches
Solution 1
The simplest way is to do:
git push --all origin
This will push tags and branches.
Solution 2
With modern git you always fetch all branches (as remote-tracking branches into refs/remotes/origin/*
namespace, visible with git branch -r
or git remote show origin
).
By default (see documentation of push.default
config variable) you push matching branches, which means that first you have to do git push origin branch
for git to push it always on git push
.
If you want to always push all branches, you can set up push refspec. Assuming that the remote is named origin
you can either use git config:
$ git config --add remote.origin.push '+refs/heads/*:refs/heads/*'
$ git config --add remote.origin.push '+refs/tags/*:refs/tags/*'
or directly edit .git/config
file to have something like the following:
[remote "origin"] url = [email protected]:/srv/git/repo.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/tags/*:refs/tags/* push = +refs/heads/*:refs/heads/* push = +refs/tags/*:refs/tags/*
Solution 3
I had used below commands to migrate all branches to the new repository.
~$ git clone --mirror <url_of_old_repo>
~$ cd <name_of_old_repo>
~$ git remote add new-origin <url_of_new_repo>
~$ git push new-origin master
~$ git push new-origin --mirror
NOTE: I had to use second last (i.e. push master first) command while cloning a repo from Atlassian Stash to AWS CodeCommit (blank repo). I am not sure the reason, but after pushing (git push new-origin --mirror
) default branch was referring to some other branch than master
.
Solution 4
Including the + in the push spec is probably a bad idea, as it means that git will happily do a non-fast-forward push even without -f, and if the remote server is set up to accept those, you can lose history.
Try just this:
$ git config --add remote.origin.push 'refs/heads/*:refs/heads/*'
$ git config --add remote.origin.push 'refs/tags/*:refs/tags/*'
$ git config --add remote.origin.fetch 'refs/heads/*:refs/remotes/origin/*'
$ git config --add remote.origin.fetch 'refs/tags/*:refs/tags/*'
Solution 5
If you are moving branches to a new repo from an old one and do NOT have all the old repo branches local, you will need to track them first.
for remote in `git branch -r | grep -v '\->'`; do git branch --track $remote; done
Then add your new remote repo:
git remote add bb <path-to-new-repo>
Then you can push all using this command:
git push -u bb --all
Or you can configure the repo using the git config commands noted in the other responses here if you are not doing this one time or are only looking to move local branches.
The important point, the other responses only push all LOCAL branches. If the branches only exist on an alternate REMOTE repository they will not move without tracking them first. The for loop presented here will help with that.
lprsd
I am a curious learner! You should follow me on twitter as @lprsd_
Updated on July 31, 2022Comments
-
lprsd almost 2 years
I'd like to push and pull all the branches by default, including the newly created ones.
Is there a setting that I can define for it?
Otherwise, when I add a new branch, locally and I want to pull it from the server, what is the simplest way to do it?
I created a new branch with the same name and tried to pull but it doesn't work. Asks me for all the remote config of the branch. How do I set it.
-
Ether over 12 yearsYou can also add the
--global
option to each of these to make this the global default for all your repositories. -
Ether over 12 yearsIt is unfortunate that the + is added automatically by git when doing
git remote add
. -
András Szepesházi almost 12 yearsAmong dozens of answers that I found on SO and other places, this is the simplest way to push a newly created local branch, without touching configuration. Thanks!
-
Alec almost 12 yearsAnd if you add
-u
once, e.g.git push --all origin -u
, tracking is setup and after that you can simply usegit push
. -
thisgeek over 11 yearsFor git version 1.7.12.3 I had to use
git push --tags origin
to push all tags. -
Lance Cleveland about 11 yearsBTW, I am using "bb" in place of "origin" here because I assume your original/old repository was named "origin" and is likely still attached to that label. "bb" is for Bitbucket, where I moved my original repo to, but you can call it something more applicable like "neworigin" if you prefer.
-
Ahmed Jihad almost 11 yearsAlso look at "--mirror" instead of "--all" this push more stuff
-
Scott over 10 years@Loda - Is "--mirror" going to do what the poster wants? We had a guy vaporize all branches on the remote repo with "--mirror" (he only had master checked out when he did "--mirror"). I suspect there's a way to fix up your refs/remote/* but didn't have time to play with it and ended up restoring from backup.
-
Ahmed Jihad over 10 years@Scott "--mirror" does more stuff than just pushing news branch. and it does it differently. (ie: force the update, delete old branch, ...). It does more than what the poster asked. Anyway, according to stackoverflow.com/q/3333102/154272 and kernel.org/pub/software/scm/git/docs/git-push.html, this is probably NOT the best way to go.
-
Anonigan over 10 years@Merc:
git push --all origin
is good for one time publishing all branches and tags, though default up till current version 'matching' semantic would mean that you would push all branches afterwards... unless you add new branch or tag. The setting to "push [...] all the branches by default" is as written. -
asmaier over 10 yearsTo also push the tags from 1.8.3 on you can use
git push --all --follow-tags origin
-
Dereckson about 10 yearsYou could improve the answer to add the way to reconfigure Git this way. This is useful for users having set the simple mode.
-
Jack about 10 yearsWARNING: If you have a bunch of LOCAL branches that you have not cleaned up (features, hotfix's) - or did not clean up properly (me), this will flood your remote. Damn. And we just did a pruning. Not sure why my local had so many branches left over.
-
Matej about 10 yearsIt didn't push tags for me.. I had to do
git push --tags origin
as well -
mike over 9 yearsThis has changed since git 2.0. Push default is simple, not matching any more.
-
jhsowter over 9 yearsThat didn't work for me. Ended up with all remote branches tracking the same local branch :/
-
foxundermon almost 9 yearsλ git fetch --all origin fatal: fetch --all does not take a repository argument
-
tokhi almost 9 yearsare you trying
git fetch --all
? -
Brian Lacy over 8 yearsI tried this and got an error on push:
fatal: Invalid refspec ''+refs/heads/*:refs/heads/*''
(Note: I'm on git 2.0. I'm still working out how to fix this.) -
Anonigan over 8 years@BrianLacy it looks to me like you have quotes around the refspec in the configuration. Just open config file in editor and check.
-
Alessandro Fazzi over 8 yearsAFAIK this shouldn't work, as per @jhsowter comment. the right command for me to track a remote branch in a newly cloned repo is
git branch --track reponame origin/reponame
otherwise you'll get all the remote branches tracked on the current local branch -
Minhaj over 8 yearsto push all local branches, git push <remote> --all
-
hasanghaforian about 8 yearsNow default value for
push.default
issimple
. -
Pelmered almost 8 yearsPerfect for moving a repo to another host. Thank you!
-
yanzi1225627 over 7 yearsThis is indeed only useful method. Use
git push new_origin --all
just push your current local branches to new_origin, not all branches of origin. -
jmmut over 7 yearsJust noting that this makes a
--bare
repository, which is a bit different from a regular repository, it only has the.git
files, not your files. It's perfectly enough if you are not going to do work in it. See--bare
and--mirror
git-scm.com/docs/git-clone. -
Ivan over 7 yearsOn my git 1.9.5 git push --all origin does not push all tags. It pushes all branches.
-
Ivan over 7 yearsA separate git push --tags origin is required to push the tags. This can be verified easily as when git push --tags origin is rerun the behaviour is different, saying it is all up to date.
-
wayofthefuture about 7 yearsWe were trying to move our repo, but only had checked out a couple of the branches. Origin had all the branches. git push --all seems to push only the local branches and not the tracking branches. So I guess you have to checkout all branches before trying to push.
-
SanthoshM almost 7 yearsAll though it only has the .git files and not the actual source code, if you perform a remote update it will re-fetch everything from the origin to the destination.
-
vladkras about 6 yearsthis doesn't push all branches (as OP asked) but only all local branches, so for me this doesn't work cause I want to copy all branches from one repo to another via local repo, but only master (checkouted) and local branches are pushed
-
brimble2010 about 6 years@vladkras what other branches would you be aiming to push other than local ones?
-
brimble2010 about 6 years@vladkras sorry, I didn't read your comment correctly and now cannot edit. My response: this is because git is distributed so any commands that you issue are to interact with your local set of branches and tags. As you mentioned, you'd have to have all of the branches and tags locally for this command to work. I would say that this is not something that people would do often.
-
Paul Hicks almost 6 yearsI changed the repo-collecting snippet to
git branch -r | grep -v '\->' | sed 's/ origin\///'
, which gives just the remote branch name. -
Toddius Zho almost 6 yearsThis was a lifesaver! This "master before mirror" method fixed issue with Bitbucket being the destination and believing a different branch other than "master" was the main branch.
-
Nitin Kumar about 2 yearsThis did not work for me for some reason... Maybe because my repository was private. Anyways I found more info here: docs.github.com/en/repositories/… and used the step, which is pretty much the same and it worked. Just FYI, had made my source and target, public.