How to clone all remote branches in Git?

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

    Why you only see "master"

    git clone downloads all remote branches but still considers them "remote", even though the files are located in your new repository. There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master". By default, git branch only shows local branches, which is why you only see "master".

    git branch -a shows all branches, including remote branches.


    How to get local branches

    If you actually want to work on a branch, you'll probably want a "local" version of it. To simply create local branches from remote branches (without checking them out and thereby changing the contents of your working directory), you can do that like this:

    git branch branchone origin/branchone
    git branch branchtwo origin/branchtwo
    git branch branchthree origin/branchthree
    

    In this example, branchone is the name of a local branch you're creating based on origin/branchone; if you instead want to create local branches with different names, you can do this:

    git branch localbranchname origin/branchone
    

    Once you've created a local branch, you can see it with git branch (remember, you don't need -a to see local branches).

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

    None of these answers cut it, except user nobody is on the right track.

    I was having trouble with moving a repo from one server/system to another. When I cloned the repo, it only created a local branch for master so when I pushed to the new remote, only master branch was pushed.

    So I found these two methods VERY useful. Hope they help someone else.

    Method 1:

    git clone --mirror OLD_REPO_URL
    cd new-cloned-project
    mkdir .git
    mv * .git
    git config --local --bool core.bare false
    git reset --hard HEAD
    git remote add newrepo NEW_REPO_URL
    git push --all newrepo
    git push --tags newrepo
    

    Method 2:

    git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
    git clone OLD_REPO_URL
    cd new-cloned-project
    git clone-branches
    git remote add newrepo NEW_REPO_URL
    git push --all newrepo
    git push --tags newrepo
    
    0 讨论(0)
  • 2020-11-22 01:53

    You can easily switch to a branch without using the fancy "git checkout -b somebranch origin/somebranch" syntax. You can do:

    git checkout somebranch
    

    Git will automatically do the right thing:

    $ git checkout somebranch
    Branch somebranch set up to track remote branch somebranch from origin.
    Switched to a new branch 'somebranch'
    

    Git will check whether a branch with the same name exists in exactly one remote, and if it does, it tracks it the same way as if you had explicitly specified that it's a remote branch. From the git-checkout man page of Git 1.8.2.1:

    If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

    $ git checkout -b <branch> --track <remote>/<branch>
    
    0 讨论(0)
  • 2020-11-22 01:54

    As of early 2017, the answer in this comment works:

    git fetch <origin-name> <branch-name> brings the branch down for you. While this doesn't pull all branches at once, you can singularly execute this per-branch.

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

    If you have many remote branches that you want to fetch at once, do:

    $ git pull --all
    

    Now you can checkout any branch as you need to, without hitting the remote repository.


    Note: This will not create working copies of any non-checked out branches, which is what the question was asking. For that, see

    • https://stackoverflow.com/a/4754797/430062
    • https://stackoverflow.com/a/7216269/430062
    0 讨论(0)
  • 2020-11-22 01:55
    #!/bin/bash
    for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
       git branch --track ${branch#remotes/origin/} $branch
    done
    

    These code will pull all remote branches code to local repo.

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