How to clone all remote branches in Git?

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

    Cloning from a local repo will not work with git clone & git fetch: a lot of branches/tags will remain unfetched.

    To get a clone with all branches and tags.

    git clone --mirror git://example.com/myproject myproject-local-bare-repo.git
    

    To get a clone with all branches and tags but also with a working copy:

    git clone --mirror git://example.com/myproject myproject/.git
    cd myproject
    git config --unset core.bare
    git config receive.denyCurrentBranch updateInstead
    git checkout master
    
    0 讨论(0)
  • 2020-11-22 01:47

    Here's an answer that uses awk. This method should suffice if used on a new repo.

    git branch -r | awk -F/ '{ system("git checkout " $NF) }'
    

    Existing branches will simply be checked out, or declared as already in it, but filters can be added to avoid the conflicts.

    It can also be modified so it calls an explicit git checkout -b <branch> -t <remote>/<branch> command.

    This answer follows Nikos C.'s idea.


    Alternatively we can specify the remote branch instead. This is based on murphytalk's answer.

    git branch -r | awk '{ system("git checkout -t " $NF) }'
    

    It throws fatal error messages on conflicts but I see them harmless.


    Both commands can be aliased.

    Using nobody's answer as reference, we can have the following commands to create the aliases:

    git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
    git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'
    

    Personally I'd use track-all or track-all-branches.

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

    The fetch that you are doing should get all the remote branches, but it won't create local branches for them. If you use gitk, you should see the remote branches described as "remotes/origin/dev" or something similar.

    To create a local branch based on a remote branch, do something like:

    git checkout -b dev refs/remotes/origin/dev

    Which should return something like:

    Branch dev set up to track remote branch refs/remotes/origin/dev.
    Switched to a new branch "dev"

    Now, when you are on the dev branch, "git pull" will update your local dev to the same point as the remote dev branch. Note that it will fetch all branches, but only pull the one you are on to the top of the tree.

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

    all the answers I saw here are valid but there is a much cleaner way to clone a repository and to pull all the branches at once.

    When you clone a repository all the information of the branches is actually downloaded but the branches are hidden. With the command

    $ git branch -a
    

    you can show all the branches of the repository, and with the command

    $ git checkout -b branchname origin/branchname
    

    you can then "download" them manually one at a time.


    However, when you want to clone a repo with a lot of branches all the ways illustrated are above are lengthy and tedious in respect to a much cleaner and quicker way that I am going to show, though it's a bit complicated. You need three steps to accomplish this:

    1. First step

    create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:

    $ cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder
    $ git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git
    

    the local repository inside the folder my_repo_folder is still empty, there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.

    1. Second step

    switch this repository from an empty (bare) repository to a regular repository by switching the boolean value "bare" of the git configurations to false:

    $ git config --bool core.bare false
    
    1. Third Step

    Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repo.

    $ git reset --hard
    

    So now you can just type the command "git branch" and you can see that all the branches are downloaded.

    This is the quick way in which you can clone a git repository with all the branches at once, but it's not something you wanna do for every single project in this way.

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

    Use aliases. Though there aren't any native Git one-liners, you can define your own as

    git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
    

    and then use it as

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

    First, clone a remote Git repository and cd into it:

    $ git clone git://example.com/myproject
    $ cd myproject
    

    Next, look at the local branches in your repository:

    $ git branch
    * master
    

    But there are other branches hiding in your repository! You can see these using the -a flag:

    $ git branch -a
    * master
      remotes/origin/HEAD
      remotes/origin/master
      remotes/origin/v1.0-stable
      remotes/origin/experimental
    

    If you just want to take a quick peek at an upstream branch, you can check it out directly:

    $ git checkout origin/experimental
    

    But if you want to work on that branch, you'll need to create a local tracking branch which is done automatically by:

    $ git checkout experimental
    

    and you will see

    Branch experimental set up to track remote branch experimental from origin.
    Switched to a new branch 'experimental'
    

    Here, "new branch" simply means that the branch is taken from the index and created locally for you. As the previous line tells you, the branch is being set up to track the remote branch, which usually means the origin/branch_name branch.

    Now, if you look at your local branches, this is what you'll see:

    $ git branch
    * experimental
      master
    

    You can actually track more than one remote repository using git remote.

    $ git remote add win32 git://example.com/users/joe/myproject-win32-port
    $ git branch -a
    * master
      remotes/origin/HEAD
      remotes/origin/master
      remotes/origin/v1.0-stable
      remotes/origin/experimental
      remotes/win32/master
      remotes/win32/new-widgets
    

    At this point, things are getting pretty crazy, so run gitk to see what's going on:

    $ gitk --all &
    
    0 讨论(0)
提交回复
热议问题