How to fetch all Git branches

后端 未结 30 1012
情书的邮戳
情书的邮戳 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:58
    |‾‾‾‾‾‾‾‾‾‾‾‾‾fetch/clone‾‾‾‾‾‾‾‾‾‾‾‾↓   |‾‾‾‾‾‾‾‾‾‾‾‾checkout‾‾‾‾‾‾‾‾‾‾↓   
    |‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾pull‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾↓
    Remote repository (`origin`) <=> Local repository <=> Index <=> Workspace
    ↑_________________push_______________|   ↑____commit____|  ↑____add_____| 
    
    
    # 拉取远程仓库所有分支信息 → 本地仓库
    # fetch all remote repository branch meta → local repository
    git remote set-branches origin '*'
    git fetch -v
    
    # 把所有远程分支数据搞到本地
    # fetch all remote repository branch data → local repository
    git branch -r | grep -v '\->' | while read remote; do git branch "${remote#origin/}" "$remote"; done
    git fetch --all
    git pull --all
    
    0 讨论(0)
  • 2020-11-22 09:58

    Based on the answer by Learath2, here's what I did after doing git clone [...] and cd-ing into the created directory:

    git branch -r | grep -v master | awk {print\$1} | sed 's/^origin\/\(.*\)$/\1 &/' | xargs -n2 git checkout -b

    Worked for me but I can't know it'll work for you. Be careful.

    0 讨论(0)
  • 2020-11-22 09:59

    To list remote branches:
    git branch -r

    You can check them out as local branches with:
    git checkout -b LocalName origin/remotebranchname

    0 讨论(0)
  • 2020-11-22 09:59

    You will need to create local branches tracking remote branches.

    Assuming that you've got only one remote called origin, this snippet will create local branches for all remote tracking ones:

    for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
    

    After that, git fetch --all will update all local copies of remote branches.

    Also, git pull --all will update your local tracking branches, but depending on your local commits and how the 'merge' configure option is set it might create a merge commit, fast-forward or fail.

    0 讨论(0)
  • 2020-11-22 09:59

    Why is noone answering the guys question and explaining what is happening?

    1. Make sure you are tracking all of the remote branches (or whichever ones you want to track).
    2. Update your local branches to reflect the remote branches.

    Track all remote branches:

    Track all branches that exist in the remote repo.

    Manually do it:

    You would replace <branch> with a branch that is displayed from the output of git branch -r.

    git branch -r
    git branch --track <branch>
    

    Do it with a bash script

    for i in $(git branch -r | grep -vE "HEAD|master"); do git branch --track ${i#*/} $i; done
    

    Update information about the remote branches on your local computer:

    This fetches updates on branches from the remote repo which you are tracking in your local repo. This does not alter your local branches. Your local git repo is now aware of things that have happened on the remote repo branches. An example would be that a new commit has been pushed to the remote master, doing a fetch will now alert you that your local master is behind by 1 commit.

    git fetch --all
    

    Update information about the remote branches on your local computer and update local branches:

    Does a fetch followed by a merge for all branches from the remote to the local branch. An example would be that a new commit has been pushed to the remote master, doing a pull will update your local repo about the changes in the remote branch and then it will merge those changes into your local branch. This can create quite a mess due to merge conflicts.

    git pull --all
    
    0 讨论(0)
  • 2020-11-22 09:59

    For Windows users using PowerShell:

    git branch -r | ForEach-Object {
        # Skip default branch, this script assumes
        # you already checked-out that branch when cloned the repo
        if (-not ($_ -match " -> ")) {
            $localBranch = ($_ -replace "^.*?/", "")
            $remoteBranch = $_.Trim()
            git branch --track "$localBranch" "$remoteBranch"
        }
    }
    git fetch --all
    git pull --all
    
    0 讨论(0)
提交回复
热议问题