How to fetch all Git branches

后端 未结 30 1014
情书的邮戳
情书的邮戳 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.

    0 讨论(0)
  • 2020-11-22 10:18
    $ git remote update
    $ git pull --all
    

    This assumes all branches are tracked.

    If they aren't you can fire this in Bash:

    for remote in `git branch -r `; do git branch --track $remote; done
    

    Then run the command.

    0 讨论(0)
  • 2020-11-22 10:18

    Use git fetch && git checkout RemoteBranchName.

    It works very well for me...

    0 讨论(0)
  • 2020-11-22 10:18

    Just these three commands will get all the branches:

    git clone --mirror repo.git  .git     (gets just .git  - bare repository)
    
    git config --bool core.bare false
    
    git reset --hard
    
    0 讨论(0)
  • 2020-11-22 10:19

    If you have problems with fetch --all then track your remote branch:

    git checkout --track origin/%branchname%
    
    0 讨论(0)
  • 2020-11-22 10:19

    Here's a Perl version of the one-liner provided in the accepted answer:

    git branch -r | perl -e 'while(<>) {chop; my $remote = $_; my ($local) = ($remote =~ /origin\/(.*)/); print "git branch --track $local $remote\n";}' > some-output-file

    You can run the output file as a Shell script if you'd like.

    We deleted our Stash project repository by accident. Fortunately someone had created a fork right before the accidental loss. I cloned the fork to my local (will omit the details of how I did that). Once I had the fork fully in my local, I ran one one-liner. I modified the remote's URL (origin in my case) to point to the target repository we were recovering to:

    git remote set-url origin <remote-url>

    And finally pushed all branches to origin like so:

    git push --all origin

    and we were back in business.

    0 讨论(0)
提交回复
热议问题