Nice programing

Git에서 단일 브랜치를 어떻게 복제합니까?

nicepro 2020. 9. 29. 18:38
반응형

Git에서 단일 브랜치를 어떻게 복제합니까?


프로젝트 스켈레톤을 저장하는 데 사용하는 'skeleton'이라는 로컬 Git 저장소가 있습니다. 다양한 종류의 프로젝트에 대해 몇 가지 분기가 있습니다.

casey@agave [~/Projects/skeleton] git branch
* master
  rails
  c
  c++

새 프로젝트의 마스터 브랜치를 확인하려면 할 수 있습니다.

casey@agave [~/Projects] git clone skeleton new
Initialized empty Git repository in /Users/casey/Projects/new/.git/

그리고 모든 것이 내가 원하는 방식입니다. 특히, 새 마스터 브랜치는 스켈레톤 마스터 브랜치를 가리키며, 기본 프로젝트 설정의 변경 사항을 이동하기 위해 밀고 당길 수 있습니다.

그러나 작동하지 않는 것은 다른 분기를 복제하려는 경우입니다. 내가 원하는 브랜치 (예 : 브랜치) 만 가져 오도록 얻을 수 없으며 rails, 새 저장소에는 기본적으로 master스켈레톤 저장소의 rails브랜치로 푸시하고 가져 오는 브랜치가 있습니다.

이 작업을 수행하는 좋은 방법이 있습니까? 또는 이것은 Git이 내가 사물을 구조화하기를 바라는 방식이 아닐 수도 있으며, 나는 확실히 그것에 대해 열려 있습니다. Ruby on Rails 스켈레톤 저장소가 마스터 스켈레톤 저장소를 추적하는 여러 저장소가 있어야할까요? 그리고 Ruby on Rails 스켈레톤 저장소를 복제하는 개별 프로젝트.


참고 : git1.7.10 (2012 년 4 월)에서는 실제로 브랜치를 하나만 복제 할 수 있습니다 .

# clone only the remote primary HEAD (default: origin/master)
git clone --single-branch

as in:
git clone <url> --branch <branch> --single-branch [<folder>]

당신은 그것을 볼 수 있습니다 t5500-fetch-pack.sh:

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

Tobu는 다음과 같이 설명 합니다.

이는 얕은 클론을 수행 할 때 암시 적입니다.
이것은 git clone --depth 1대역폭을 절약하는 가장 쉬운 방법입니다.

그리고 Git 1.9.0 (2014 년 2 월)부터 얕은 클론은 데이터 전송 (푸시 / 풀)을 지원하므로이 옵션은 이제 훨씬 더 유용합니다.
더 "에서 참조 git clone --depth 1(얕은 클론)은 밖으로 만드는 것보다 더 유용? ".


얕은 클론의 "실행 취소"는 " 얕은 클론을 전체 클론으로 변환 "(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

Chris는 다음과 같이 말합니다.

누락 된 분기를 되 --single-branch돌리는 매직 라인 은 (git v2.1.4)입니다.

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

한 가지 방법은 다음을 실행하는 것입니다.

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. 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

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.


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.


You can do it by using the below command:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in

From git-clone man page:

--single-branch is your friend during clone remember to use with --branch <branch name> or only remote primary HEAD will be cloned (master by default)

Always remember to do Ctrl + F5 to read fresh source, not the one from cache :-) (I didn't so didn't know about this option for long time.)


git clone <url> --branch <branch> --single-branch

Just put in URL and branch name.


Clone only one branch. This is the easiest way:

$ git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git

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>

For cloning a specific branch you can do :

git clone --branch yourBranchName git@yourRepository.git


A little late but I wanted to add the solution I used to solve this problem. I found the solution here.

Anyway, the question seems to be asking 'how to start a new project from a branch of another repo?'

To this, the solution I used would be to first create a new repo in github or where ever. This will serve as the repo to your new project.

On your local machine, navigate to the project that has the branch you want to use as the template for your new project.

Run the command:

git push https://github.com/accountname/new-repo.git +old_branch:master

What this will do is push the old_branch to new-repo and make it the master branch of the new repo.

You then just have to clone the new repo down to your new project's local directory and you have a new project started at the old branch.


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.


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

Can be done in 2 steps

  1. Clone the repository

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

    • git checkout $BranchName

  1. Open Git Bash shell.
  2. Create an directory in your file system where you want to checkout.
    • $ mkdir Feature_develop_branch
  3. Change directory to Feature_develop_branch folder.
    • $ cd Feature_develop_branch
  4. Clone the repository using external clone URL.
  5. After cloning, change the directory to created repositoryName.
    • $ cd /repositoryName
  6. Check out the branch.
    • $ git checkout <Branch Name>

참고URL : https://stackoverflow.com/questions/1778088/how-do-i-clone-a-single-branch-in-git

반응형