Search code examples
gitgit-branchgit-clone

How to Git clone and fetch all branches from a GitHub repository to my local machine?


I'm trying to clone all branches from a GitHub repository to my local machine using Git. I want to have local copies of all branches, not just the default branch.

I've tried the following commands:

git clone https://github.com/abdurrahimcs50/Django-Boilerplate-Project.git
cd Django-Boilerplate-Project
git fetch --all

Then, for each remote branch, I tried to create a local tracking branch using this loop:

for branch in `git branch -r | grep -v HEAD`;do
    git checkout --track $branch || true
done

I expected that after running these commands, I would have local copies of all branches. However, when I list the branches using git branch, only the default branch is listed. The other branches are listed when I use git branch --all, but they are prefixed with remotes/origin/, indicating that they are remote tracking branches, not local branches.

What am I doing wrong? How can I clone and download all branches from a GitHub repository to my local machine?


Solution

  • You can do git fetch --all to fetch all the branches from the remote repository

    and then git checkout <remote branch name> to switch to the specific branch you want to work on.