How to clone all remote branches in Git?

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

    Looking at one of answers to the question I noticed that it's possible to shorten it:

    for branch in  `git branch -r | grep -v 'HEAD\|master'`; do  
     git branch --track ${branch##*/} $branch;
    done
    

    But beware, if one of remote branches is named as e.g. admin_master it won't get downloaded!

    Thanks to bigfish for original idea

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

    Use my tool git_remote_branch (you need Ruby installed on your machine). It's built specifically to make remote branch manipulations dead easy.

    Each time it does an operation on your behalf, it prints it in red at the console. Over time, they finally stick into your brain :-)

    If you don't want grb to run commands on your behalf, just use the 'explain' feature. The commands will be printed to your console instead of executed for you.

    Finally, all commands have aliases, to make memorization easier.

    Note that this is alpha software ;-)

    Here's the help when you run grb help:

    git_remote_branch version 0.2.6
    
      Usage:
    
      grb create branch_name [origin_server] 
    
      grb publish branch_name [origin_server] 
    
      grb rename branch_name [origin_server] 
    
      grb delete branch_name [origin_server] 
    
      grb track branch_name [origin_server] 
    
    
    
      Notes:
      - If origin_server is not specified, the name 'origin' is assumed 
        (git's default)
      - The rename functionality renames the current branch
    
      The explain meta-command: you can also prepend any command with the 
    keyword 'explain'. Instead of executing the command, git_remote_branch 
    will simply output the list of commands you need to run to accomplish 
    that goal.
    
      Example: 
        grb explain create
        grb explain create my_branch github
    
      All commands also have aliases:
      create: create, new
      delete: delete, destroy, kill, remove, rm
      publish: publish, remotize
      rename: rename, rn, mv, move
      track: track, follow, grab, fetch
    
    0 讨论(0)
  • 2020-11-22 01:40

    Using the --mirror option seems to copy the remote tracking branches properly. However, it sets up the repository as a bare repository, so you have to turn it back into a normal repository afterwards.

    git clone --mirror path/to/original path/to/dest/.git
    cd path/to/dest
    git config --bool core.bare false
    git checkout anybranch
    

    Reference: Git FAQ: How do I clone a repository with all remotely tracked branches?

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

    This Bash script helped me out:

    #!/bin/bash
    for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
        git branch --track "${branch##*/}" "$branch"
    done
    

    It will create tracking branches for all remote branches, except master (which you probably got from the original clone command). I think you might still need to do a

    git fetch --all
    git pull --all
    

    to be sure.

    One liner: git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
    As usual: test in your setup before copying rm -rf universe as we know it

    Credits for one-liner go to user cfi

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

    When you do "git clone git://location", all branches and tags are fetched.

    In order to work on top of a specific remote branch, assuming it's the origin remote:

    git checkout -b branch origin/branchname
    
    0 讨论(0)
  • 2020-11-22 01:42

    Regarding,

    $ git checkout -b experimental origin/experimental

    using

    $ git checkout -t origin/experimental
    

    or the more verbose but easier to remember

    $ git checkout --track origin/experimental
    

    might be better, in terms of tracking a remote repository.

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