How to fetch all Git branches

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

    I wrote a little script to manage cloning a new repo and making local branches for all the remote branches.

    You can find the latest version here:

    #!/bin/bash
    
    # Clones as usual but creates local tracking branches for all remote branches.
    # To use, copy this file into the same directory your git binaries are (git, git-flow, git-subtree, etc)
    
    clone_output=$((git clone "$@" ) 2>&1)
    retval=$?
    echo $clone_output
    if [[ $retval != 0 ]] ; then
        exit 1
    fi
    pushd $(echo $clone_output | head -1 | sed 's/Cloning into .\(.*\).\.\.\./\1/') > /dev/null 2>&1
    this_branch=$(git branch | sed 's/^..//')
    for i in $(git branch -r | grep -v HEAD); do
      branch=$(echo $i | perl -pe 's/^.*?\///')
      # this doesn't have to be done for each branch, but that's how I did it.
      remote=$(echo $i | sed 's/\/.*//')
      if [[ "$this_branch" != "$branch" ]]; then
          git branch -t $branch $remote/$branch
      fi
    done
    popd > /dev/null 2>&1
    

    To use it, just copy it into your git bin directory (for me, that’s C:\Program Files (x86)\Git\bin\git-cloneall), then, on the command line:

    git cloneall [standard-clone-options] 
    

    It clones as usual, but creates local tracking branches for all remote branches.

提交回复
热议问题