How to fetch all Git branches

后端 未结 30 1058
情书的邮戳
情书的邮戳 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: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 with a branch that is displayed from the output of git branch -r.

    git branch -r
    git branch --track 
    

    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
    

提交回复
热议问题