How do I clone a single branch in Git?

后端 未结 21 2534
再見小時候
再見小時候 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:36

    Similar to what @nosaiba-darwish said here: here

    This is what we usually do in our company:

    git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally
    
    0 讨论(0)
  • 2020-11-22 00:37

    One way is to execute the following.

    git clone user@git-server:project_name.git -b branch_name /your/folder
    

    Where branch_name is the branch of your choice and "/your/folder" is the destination folder for that branch. It's true that this will bring other branches giving you the opportunity to merge back and forth.

    Update

    Now, starting with Git 1.7.10, you can now do this

    git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder
    
    0 讨论(0)
  • 2020-11-22 00:37

    Let us take the example of flask repo. It has 3 branches in addition to master. Let us checkout the 1.1.x remote branch

    clone the git repo

    git clone https://github.com/pallets/flask
    

    cd into the repo.

    cd flask
    

    fetch remote branch 1.1.x

    git fetch origin 1.1.x
    

    checkout the branch

    git checkout 1.1.x
    

    You will switch to the branch 1.1.x and it will track the remote 1.1.x branch.

    0 讨论(0)
  • 2020-11-22 00:38
    git clone --branch {branch-name} {repo-URI}
    

    Example:

    git clone --branch dev https://github.com/ann/cleaningmachine.git
    
    • dev: This is the {branch-name}
    • https://github.com/ann/cleaningmachine.git: This is the {repo-URI}
    0 讨论(0)
  • 2020-11-22 00:39

    Using Git version 1.7.3.1 (on Windows), here's what I do ($BRANCH is the name of the branch I want to checkout and $REMOTE_REPO is the URL of the remote repository I want to clone from):

    mkdir $BRANCH
    cd $BRANCH
    git init
    git remote add -t $BRANCH -f origin $REMOTE_REPO
    git checkout $BRANCH
    

    The advantage of this approach is that subsequent git pull (or git fetch) calls will also just download the requested branch.

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

    You can try the long-winded way:

    mkdir newrepo.git
    cd newrepo.git
    git init
    git remote add origin file:///path/to/original
    git fetch origin branchiwant:refs/remotes/origin/branchiwant
    git checkout -b branchiwant --track origin/branchiwant
    

    What this does is:

    • Create and init an empty Git repository.
    • Adds the original repository as a remote called origin.
    • Fetches only the branch you require from the remote called origin.
    • Creates and checks out a new branch that is set up to track the source branch you just cloned.

    Hopefully that will be something like what you are after.

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