How to fetch all Git branches

后端 未结 30 1551
情书的邮戳
情书的邮戳 2020-11-22 09:38

I cloned a Git repository, which contains about five branches. However, when I do git branch I only see one of them:

$ git branch
* master
         


        
30条回答
  •  一向
    一向 (楼主)
    2020-11-22 10:17

    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, there is a much cleaner and quicker way, 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.

    2. 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
      
    3. 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.

提交回复
热议问题