How to clone all remote branches in Git?

后端 未结 30 2074
情话喂你
情话喂你 2020-11-22 01:08

I have a master and a development branch, both pushed to GitHub. I\'ve cloned, pulled, and fetched, but I re

30条回答
  •  囚心锁ツ
    2020-11-22 01:49

    all the answers I saw here are valid but there is a much cleaner way to clone a repository and to pull all the branches at once.

    When you clone a repository all the information of the branches is actually downloaded but the branches are hidden. With the command

    $ git branch -a
    

    you can show all the branches of the repository, and with the command

    $ git checkout -b branchname origin/branchname
    

    you can then "download" them manually one at a time.


    However, when you want to clone a repo with a lot of branches all the ways illustrated are above are lengthy and tedious in respect to a much cleaner and quicker way that I am going to show, though it's a bit complicated. You need three steps to accomplish this:

    1. First step

    create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:

    $ cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder
    $ git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git
    

    the local repository inside the folder my_repo_folder is still empty, there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.

    1. Second step

    switch this repository from an empty (bare) repository to a regular repository by switching the boolean value "bare" of the git configurations to false:

    $ git config --bool core.bare false
    
    1. Third Step

    Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repo.

    $ git reset --hard
    

    So now you can just type the command "git branch" and you can see that all the branches are downloaded.

    This is the quick way in which you can clone a git repository with all the branches at once, but it's not something you wanna do for every single project in this way.

提交回复
热议问题