1

I have a repository in github with X number of branches and a repository in a closed network.

I need to move all of the repository to the one on the closed network. How do I clone all the branches (not just the default master), so I could then push it to the closed-network one with all the branches existing.

Entering the repo binary is not the issue here.

I want the same result as if I cloned my repo and did git branch to each branch, so it would be initialized locally.

TriskalJM
  • 2,393
  • 1
  • 19
  • 20
levich
  • 628
  • 1
  • 9
  • 15
  • [Here is the detail answer click here](https://stackoverflow.com/questions/67699/how-to-clone-all-remote-branches-in-git?page=1&tab=votes#tab-top) – Suneel Kumar Jul 05 '17 at 11:13
  • Possible duplicate of [How to clone all remote branches in Git?](https://stackoverflow.com/questions/67699/how-to-clone-all-remote-branches-in-git) – Ebrahim Poursadeqi Jul 05 '17 at 11:46

4 Answers4

3

If it's ok to not create the remote branch refs, and if the clone can be "bare" (no default working tree), then you can use

git clone --mirror url

But you said you want the same result as cloning and then checking out each branch. Taken literally, that would mean a non-bare repo that has both the remote refs and the local branches. To do that you could do this:

First, clone normally and cd into the new clone

git clone url new-repo
cd new-repo

Now force detached head state to ensure no "current branch" problems

git checkout `git rev-parse HEAD`

Now get the local branches set up

git fetch origin +refs/heads/*:refs/heads/*

And now of course you can check out whatever branch you should be on, e.g.

git checkout master
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
2

Try this,

to get all branches from remote.

Bash script :

#!/bin/bash

for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
    git branch --track "${branch##*/}" "$branch"
done

or

One line code :

git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs

Muhammad Shafaf
  • 346
  • 2
  • 6
1

Once created something like that. Hopefully it still works. Perhaps it could help you: https://github.com/srfrnk/fetch-all

srfrnk
  • 2,459
  • 1
  • 17
  • 32
0

There's a git-clone option --mirror which sets up all remotely tracked branches. But, --mirror also implies --bare option, ie. the clone will will not be created as a working copy and you cannot checkout the branches. Therefore the directory is changed to a working directory afterwards.

$ git clone --mirror original-repo.git /path/cloned-directory/.git
$ cd /path/cloned-directory
$ git config --bool core.bare false
$ git checkout anybranch

Git FAQ: How do I clone a repository with all remotely tracked branches?

conspicillatus
  • 195
  • 3
  • 11
  • 1
    Careful: if you use the four lines shown above, your `fetch` configuration entry will still be set up to *overwrite* local branches. – torek Jul 05 '17 at 15:04