How do I clone a single branch in Git?

后端 未结 21 2414
再見小時候
再見小時候 2020-11-22 00:09

I have a local Git repository called \'skeleton\' that I use for storing project skeletons. It has a few branches, for different kinds of projects:

casey@aga         


        
相关标签:
21条回答
  • 2020-11-22 00:28

    Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:

    # clone only the remote primary HEAD (default: origin/master)
    git clone <url> --single-branch
    
    # as in:
    git clone <url> --branch <branch> --single-branch [<folder>]
    

    You can see it in t5500-fetch-pack.sh:

    test_expect_success 'single branch clone' '
      git clone --single-branch "file://$(pwd)/." singlebranch
    '
    

    Tobu comments that:

    This is implicit when doing a shallow clone.
    This makes git clone --depth 1 the easiest way to save bandwidth.

    And since Git 1.9.0 (February 2014), shallow clones support data transfer (push/pull), so that option is even more useful now.
    See more at "Is git clone --depth 1 (shallow clone) more useful than it makes out?".


    "Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (git 1.8.3+)

    # unshallow the current branch
    git fetch --unshallow
    
    # for getting back all the branches (see Peter Cordes' comment)
    git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
    git fetch --unshallow
    

    As Chris comments:

    the magic line for getting missing branches to reverse --single-branch is (git v2.1.4):

    git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
    git fetch --unshallow  
    

    With Git 2.26 (Q1 2020), "git clone --recurse-submodules --single-branch" now uses the same single-branch option when cloning the submodules.

    See commit 132f600, commit 4731957 (21 Feb 2020) by Emily Shaffer (nasamuffin).
    (Merged by Junio C Hamano -- gitster -- in commit b22db26, 05 Mar 2020)

    clone: pass --single-branch during --recurse-submodules

    Signed-off-by: Emily Shaffer
    Acked-by: Jeff King

    Previously, performing "git clone --recurse-submodules --single-branch" resulted in submodules cloning all branches even though the superproject cloned only one branch.

    Pipe --single-branch through the submodule helper framework to make it to 'clone' later on.

    0 讨论(0)
  • 2020-11-22 00:28

    Can be done in 2 steps

    1. Clone the repository

      git clone <http url>
      
    2. Checkout the branch you want

      git checkout $BranchName
      
    0 讨论(0)
  • 2020-11-22 00:29

    Open the cmd.

    cd folder_name  # enter the path where to clone the branch
    

    Just one command:

    git clone url_of_projecturltoclone -b branch_name
    
    0 讨论(0)
  • 2020-11-22 00:31
    git clone <url> --branch <branch> --single-branch
    

    Just put in URL and branch name.

    0 讨论(0)
  • 2020-11-22 00:33

    For cloning a branch of Git you don't have the public key to, use this:

    git clone -b <branch> <Git repository URL or clone URL you get from Git repository>
    
    0 讨论(0)
  • 2020-11-22 00:34

    You can do it by using the below command:

    git clone -b branch_name --single-branch project_url local_folder_to_clone_in
    
    0 讨论(0)
提交回复
热议问题