How to clone all remote branches in Git?

后端 未结 30 2105
情话喂你
情话喂你 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:29

    Better late than never, but here is the best way to do this:

    mkdir repo
    cd repo
    git clone --bare path/to/repo.git .git
    git config --unset core.bare
    git reset --hard
    

    At this point you have a complete copy of the remote repo with all of it's branches (verify with git branch). You can use --mirror instead of --bare if your remote repo has remotes of its own.

    0 讨论(0)
  • 2020-11-22 01:32

    Git usually (when not specified) fetches all branches and/or tags (refs, see: git ls-refs) from one or more other repositories along with the objects necessary to complete their histories. In other words it fetches the objects which are reachable by the objects that are already downloaded. See: What does git fetch really do?

    Sometimes you may have branches/tags which aren't directly connected to the current one, so git pull --all/git fetch --all won't help in that case, but you can list them by:

    git ls-remote -h -t origin
    

    and fetch them manually by knowing the ref names.

    So to fetch them all, try:

    git fetch origin --depth=10000 $(git ls-remote -h -t origin)
    

    The --depth=10000 parameter may help if you've shallowed repository.

    Then check all your branches again:

    git branch -avv
    

    If above won't help, you need to add missing branches manually to the tracked list (as they got lost somehow):

    $ git remote -v show origin
    ...
      Remote branches:
        master      tracked
    

    by git remote set-branches like:

    git remote set-branches --add origin missing_branch
    

    so it may appear under remotes/origin after fetch:

    $ git remote -v show origin
    ...
      Remote branches:
        missing_branch new (next fetch will store in remotes/origin)
    $ git fetch
    From github.com:Foo/Bar
     * [new branch]      missing_branch -> origin/missing_branch
    

    Troubleshooting

    If you still cannot get anything other than the master branch, check the followings:

    • Double check your remotes (git remote -v), e.g.
      • Validate that git config branch.master.remote is origin.
      • Check if origin points to the right URL via: git remote show origin (see this post).
    0 讨论(0)
  • 2020-11-22 01:33

    Just do this:

    $ git clone git://example.com/myproject
    $ cd myproject
    $ git checkout branchxyz
    Branch branchxyz set up to track remote branch branchxyz from origin.
    Switched to a new branch 'branchxyz'
    $ git pull
    Already up-to-date.
    $ git branch
    * branchxyz
      master
    $ git branch -a
    * branchxyz
      master
      remotes/origin/HEAD -> origin/master
      remotes/origin/branchxyz
      remotes/origin/branch123
    

    You see, 'git clone git://example.com/myprojectt' fetches everything, even the branches, you just have to checkout them, then your local branch will be created.

    0 讨论(0)
  • 2020-11-22 01:33

    I wrote this small Powershell functions to be able to checkout all my git branches, that are on origin remote.

    Function git-GetAllRemoteBranches {
         iex "git branch -r"                       <# get all remote branches #> `
         | % { $_ -Match "origin\/(?'name'\S+)" }  <# select only names of the branches #> `
         | % { Out-Null; $matches['name'] }        <# write does names #>
    }
    
    
    Function git-CheckoutAllBranches {
        git-GetAllRemoteBranches `
            | % { iex "git checkout $_" }          <# execute ' git checkout <branch>' #>
    }
    

    More git functions can be found on my git settings repo

    0 讨论(0)
  • 2020-11-22 01:33

    I needed to do exactly the same. Here is my Ruby script.

    #!/usr/bin/env ruby
    
    local = []
    remote = {}
    
    # Prepare
    %x[git reset --hard HEAD]
    %x[git checkout master] # Makes sure that * is on master.
    %x[git branch -a].each_line do |line|
      line.strip!
      if /origin\//.match(line)
         remote[line.gsub(/origin\//, '')] = line
       else
         local << line
       end
    end
    # Update 
    remote.each_pair do |loc, rem|
      next if local.include?(loc)
      %x[git checkout --track -b #{loc} #{rem}]
    end
    %x[git fetch]
    
    0 讨论(0)
  • 2020-11-22 01:35

    You only need to use "git clone" to get all branches.

    git clone <your_http_url>
    

    Even though you only see master branch, you can use "git branch -a" to see all branches.

    git branch -a
    

    And you can switch to any branch which you already have.

    git checkout <your_branch_name>
    

    Don't worry that after you "git clone", you don't need to connect with the remote repo, "git branch -a" and "git checkout " can be run successfully when you close your wifi. So it is proved that when you do "git clone", it already has copied all branches from the remote repo. After that, you don't need the remote repo, your local already has all branches' codes.

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