Categories
git git-branch git-clone

How do I clone a specific Git branch? [duplicate]

3689

Git clone will clone remote branch into local.

Is there any way to clone a specific branch by myself without switching branches on the remote repository?

0

    2276

    git clone --single-branch --branch <branchname> <remote-repo>
    

    The --single-branch option is valid from version 1.7.10 and later.

    Please see also the other answer which many people prefer.

    You may also want to make sure you understand the difference. And the difference is: by invoking git clone --branch <branchname> url you’re fetching all the branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your frontpage, you may end up cloning 5GB of data.

    Again, that is not to say git clone --branch is not the way to accomplish that, it’s just that it’s not always what you want to accomplish, when you’re asking about cloning a specific branch.

    5

    • Comments are not for extended discussion; this conversation has been moved to chat.

      Apr 2, 2019 at 19:37

    • 2

      Pardon me, are you sure about the part you said “you’re fetching all…”? I read somewhere that git fetch doesn’t actually “copy” any files, it just fetches metadata and information about the changes. So it should be relatively light weight… Maybe you’ve used the word “fetch” literally and not from the git vocabulary?

      – aderchox

      Jul 16, 2020 at 8:29


    • 3

      @aderchox, no it will actually fetch all the content. It is pretty smart about things it transfers when you update, but when you clone a big repository it actually pulls the history, unless you explicitly tell it not to. But it will still fetch the tip of the branch. What git fetch does not — it does not check out files, but that’s not about the transfer.

      Jul 16, 2020 at 13:07

    • 6

      Often you will also want --depth 1 so that you only get the latest. This can save a lot of downloading time.

      – Eyal

      Feb 4, 2021 at 3:57

    • git clone -b branch_name --single-branch 'repo_url'

      Mar 29 at 15:37

    2276

    git clone --single-branch --branch <branchname> <remote-repo>
    

    The --single-branch option is valid from version 1.7.10 and later.

    Please see also the other answer which many people prefer.

    You may also want to make sure you understand the difference. And the difference is: by invoking git clone --branch <branchname> url you’re fetching all the branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your frontpage, you may end up cloning 5GB of data.

    Again, that is not to say git clone --branch is not the way to accomplish that, it’s just that it’s not always what you want to accomplish, when you’re asking about cloning a specific branch.

    5

    • Comments are not for extended discussion; this conversation has been moved to chat.

      Apr 2, 2019 at 19:37

    • 2

      Pardon me, are you sure about the part you said “you’re fetching all…”? I read somewhere that git fetch doesn’t actually “copy” any files, it just fetches metadata and information about the changes. So it should be relatively light weight… Maybe you’ve used the word “fetch” literally and not from the git vocabulary?

      – aderchox

      Jul 16, 2020 at 8:29


    • 3

      @aderchox, no it will actually fetch all the content. It is pretty smart about things it transfers when you update, but when you clone a big repository it actually pulls the history, unless you explicitly tell it not to. But it will still fetch the tip of the branch. What git fetch does not — it does not check out files, but that’s not about the transfer.

      Jul 16, 2020 at 13:07

    • 6

      Often you will also want --depth 1 so that you only get the latest. This can save a lot of downloading time.

      – Eyal

      Feb 4, 2021 at 3:57

    • git clone -b branch_name --single-branch 'repo_url'

      Mar 29 at 15:37

    337

    Here is a really simple way to do it 🙂

    Clone the repository

    git clone <repository_url>
    

    List all branches

    git branch -a 
    

    Checkout the branch that you want

    git checkout <name_of_branch>
    

    10

    • 1

      This switched the working directory to the correct branch, but I’m not able to push any changes I make, because I’m not “currently on a branch”.

      Jan 21, 2015 at 9:46

    • 5

      This was the solution for me, since I had already cloned ‘master’. I didn’t know I could simply ‘checkout’ a remote branch.

      – yazzer

      Jul 30, 2015 at 21:34


    • 2

      This is probably the correct way to do it; best-practices-wise

      – aaiezza

      Jul 25, 2016 at 21:01

    • 1

      This way doesn’t clone only the choosen branch. This answer seems better: stackoverflow.com/a/7349740/3075243. For example if a repo has many branches that are big enough that we don’t wanna clone each one.

      Jan 31, 2018 at 23:04

    • 4

      Very crisp answer. One additional things you have to do is: After this step: “git checkout <name_of_branch>” Do this: git branch –set-upstream-to=origin/<branch> <local_branch> Thanks.

      Apr 11, 2018 at 23:42