Set up git to pull and push all branches

280,707

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.

Share:
280,707
lprsd
Author by

lprsd

I am a curious learner! You should follow me on twitter as @lprsd_

Updated on July 31, 2022

Comments

  • lprsd
    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
    Ether over 12 years
    You can also add the --global option to each of these to make this the global default for all your repositories.
  • Ether
    Ether over 12 years
    It is unfortunate that the + is added automatically by git when doing git remote add.
  • András Szepesházi
    András Szepesházi almost 12 years
    Among 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
    Alec almost 12 years
    And if you add -u once, e.g. git push --all origin -u, tracking is setup and after that you can simply use git push.
  • thisgeek
    thisgeek over 11 years
    For git version 1.7.12.3 I had to use git push --tags origin to push all tags.
  • Lance Cleveland
    Lance Cleveland about 11 years
    BTW, 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
    Ahmed Jihad almost 11 years
    Also look at "--mirror" instead of "--all" this push more stuff
  • Scott
    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
    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
    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
    asmaier over 10 years
    To also push the tags from 1.8.3 on you can use git push --all --follow-tags origin
  • Dereckson
    Dereckson about 10 years
    You could improve the answer to add the way to reconfigure Git this way. This is useful for users having set the simple mode.
  • Jack
    Jack about 10 years
    WARNING: 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
    Matej about 10 years
    It didn't push tags for me.. I had to do git push --tags origin as well
  • mike
    mike over 9 years
    This has changed since git 2.0. Push default is simple, not matching any more.
  • jhsowter
    jhsowter over 9 years
    That didn't work for me. Ended up with all remote branches tracking the same local branch :/
  • foxundermon
    foxundermon almost 9 years
    λ git fetch --all origin fatal: fetch --all does not take a repository argument
  • tokhi
    tokhi almost 9 years
    are you trying git fetch --all ?
  • Brian Lacy
    Brian Lacy over 8 years
    I 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
    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
    Alessandro Fazzi over 8 years
    AFAIK 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
    Minhaj over 8 years
    to push all local branches, git push <remote> --all
  • hasanghaforian
    hasanghaforian about 8 years
    Now default value for push.default is simple.
  • Pelmered
    Pelmered almost 8 years
    Perfect for moving a repo to another host. Thank you!
  • yanzi1225627
    yanzi1225627 over 7 years
    This is indeed only useful method. Use git push new_origin --alljust push your current local branches to new_origin, not all branches of origin.
  • jmmut
    jmmut over 7 years
    Just 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
    Ivan over 7 years
    On my git 1.9.5 git push --all origin does not push all tags. It pushes all branches.
  • Ivan
    Ivan over 7 years
    A 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
    wayofthefuture about 7 years
    We 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
    SanthoshM almost 7 years
    All 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
    vladkras about 6 years
    this 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
    brimble2010 about 6 years
    @vladkras what other branches would you be aiming to push other than local ones?
  • brimble2010
    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
    Paul Hicks almost 6 years
    I changed the repo-collecting snippet to git branch -r | grep -v '\->' | sed 's/ origin\///', which gives just the remote branch name.
  • Toddius Zho
    Toddius Zho almost 6 years
    This 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
    Nitin Kumar about 2 years
    This 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.