How to fetch all Git branches

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

    And finally pushed all branches to origin like so:

    git push --all origin

    and we were back in business.

提交回复
热议问题