73

I'm trying to clone an entire repository onto my machine using linux. I used

git clone <url>   

I then went into the folder where it was downloaded and typed

git branch 

in the terminal. It's only showing me master and not other branches which were in the remote repository. How do I clone all branches?

I know that for each branch in the remote I can separately use

git checkout -b <name of local branch> origin/<name of remote branch>

but is there any way other than that?

sixtyTonneAngel
  • 901
  • 2
  • 8
  • 11

7 Answers7

80

(1) Inside git local repostitory, create a new sh file

touch getAllBranches.sh
vi getAllBranches.sh

(2) Insert the below content to getAllBranches.sh file:

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch
done

(3) Get all branches:

chmod +x getAllBranches.sh    
sh getAllBranches.sh

(4) Check result at local repository:

git branch

For example, I use repository: https://github.com/donhuvy/spring-boot

As you can see, I have fetched all branches to local machine:

enter image description here

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • Two comments: if one has added upstream already problems, so think the script should be "git branch -a | grep remotes | grep -v HEAD | grep -v master | grep -v upstream" (remove upstream branches). Also, why the --track? Wouldn't it be more consistent to not use that option, that is, upstream fetches and merges of master would be same as any branch? – David H Mar 19 '17 at 20:57
  • 1
    Not needed. See Xiong's answer below. – Adam Burley May 30 '18 at 12:18
58

This isn't too much complicated, very simple and straight forward steps are as follows:

After cloning the repo, run $ cd myproject

git branch -a This will show you all the remote branches.

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental

If you want to work on remote branch, you'll need to create a local tracking branch:

$ git checkout -b experimental origin/experimental

Verify whether you are in the desired branch by the following command;

$ git branch

The output will like this;

*experimental
master
some branch2
some branch3 

Notice the * sign that denotes the current branch.

Amit Gupta
  • 17,072
  • 4
  • 41
  • 53
  • 1
    I've tried this method. But I only see `master` and `experimental` branches after typing `git branch`. Anyone else has the same problem? – Brian Apr 13 '18 at 04:56
  • @Brian I'm sure you've found it by now, but the key is the `-a` switch, i.e. `git branch -a`. – Jamie S Sep 22 '18 at 16:18
23
git clone --bare <repository url goes here> .git

Then after the repo is clone with all its branches then do the following

git config --bool core.bare false

git reset --hard
Amen Ra
  • 2,843
  • 8
  • 47
  • 85
  • 2
    this worked for me but with `--mirror` instead of `--bare`. Remotes can be restored using: `for branch in $( git branch -a | sed 's/^..//' ); do git checkout $branch; git branch --set-upstream-to=$branch $branch; done ` (for some reason `$( git branch -a )` adds files in the current directory to the list as well, hence the sed) – mechalynx Mar 20 '17 at 13:32
  • 1
    `--mirror` has the same effect as `--bare` . `--mirror` creates a bare repository. – Boris EKUE-HETTAH Dec 19 '18 at 11:46
  • --bare worked for me better than --mirror, because mirror created a .git file that was basically the hidden file git creates – Juan Joya Jul 19 '19 at 20:17
  • 3
    `git reset --hard` tells me: "fatal: this operation must be run in a work tree" for both `--bare` and `--mirror` – Kirill Dec 18 '19 at 09:55
  • @Kirill same here – carlos palma Sep 18 '20 at 16:31
  • 2
    `fatal: this operation must be run in a work tree` message is because you are not correctly applying the command which written in a clearer way should be: `git clone --bare https://your_repo_name.git your_local_dir/.git`, the following commands should be executed into `your_local_dir`, and it is done. – Ουιλιαμ Αρκευα Jun 23 '21 at 15:36
17

It's only showing me master and not other branches which were in the remote repository. How do I clone all branches?

Branches are essentially pointers to commits. When you do a git clone (or a git fetch), you retrieve all of the commits from the remote repository, and all of its branches as well.

However, git branch does not show remote branches by default. Instead, it shows you your local branches, which may or may not have any relation to branches that exist on the remote. If you run git branch --all, git will report all of the branches it knows about, both local and remote.

It's worth noting that tags do not operate this way, and there is no distinction between a local and remote tag.

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
  • 2
    Your answer is helpful in understanding, but doesn't; answer the real question of how to get all the branches such that you can switch between them (for us git beginners). The selected answer would seem to do that, but if you have a better idea would love to hear it. – David H Mar 19 '17 at 20:01
  • 5
    @DavidH Generally there's nothing special you need to do; `git branch -a` will show the remote branches, and if you check out a branch name that doesn't exist locally, but does on the remote, git will automatically create a local branch for you. – Xiong Chiamiov Mar 26 '17 at 22:39
  • I'm not in a good place to test right now - I infer that if I see branches like "remotes/origin/experimental", I can then simply "git checkout experimental" and it will do as you say above? – David H Mar 27 '17 at 13:59
  • 2
    Yep, that's correct. If for some reason it doesn't (I think old versions of git didn't used to do that), you can use the longer form version that Amit mentions in his answer, which is `git checkout -b experimental origin/experimental`. – Xiong Chiamiov Mar 27 '17 at 22:26
11

I find this to be the simple solution to clone a git repository and all remote branches:

# Clone remote repository and all branches
git clone --mirror https://github.com/test/frontend.git frontend/.git

# Change into frontend directory
cd frontend

# Update git config
git config --unset core.bare

# Checkout master branch
git checkout master
Stryker
  • 5,732
  • 1
  • 57
  • 70
5
  1. git clone --bare https://repo.git projectName
  2. cd projectName
  3. git push --mirror https://repo.git

that makes your repo completely identical.

See: https://help.github.com/en/articles/duplicating-a-repository

feng zhang
  • 1,193
  • 7
  • 8
shailesh sahu
  • 157
  • 3
  • 4
3

To download a full repository, including all branches, use the following command: git clone --mirror <URI>

This will create a folder called repository.git unless you give it a different name.

Now, this gets you a full clone of the original repository, but because it's in bare=true mode, you don't have a work tree. Effectively, what you have is the .git folder, including all branches and content. This is a fancy way of saying that you won't have direct access to the files because they're stashed away within the git system (compressed, etc).

To make this a "normal" git repo, we need to make this clone the .git folder within a new folder, which will be our usual repo folder:

mkdir <repo folder name> mv repository.git <repo folder name>/.git cd <repo folder name> git checkout master

Note that there is no single native git command to download all remote branches, so the simplest way is to make sure you have all commits pushed to the origin, and then re-download the whole repository anew using this --mirror option.

togume
  • 310
  • 2
  • 7
  • 1
    This mostly worked for me, except `git checkout master` failed with `fatal: this operation must be run in a work tree`. Removing `bare=true` from `.git\config` fixed that. – Dave the Sax Feb 11 '19 at 11:08