Git

[Git]repository에서 필요한 하위 디렉토리만 clone(git 2.25 이후)

코린이탈출기 2024. 3. 17. 09:29
728x90

 
 

 

Sparse checkout

sparse checkout은 Git 저장소에서 필요한 파일이나 폴더만을 작업 디렉토리로 체크아웃하는 기능을 제공한다. 일반적으로 Git에서 저장소를 클론 하거나 체크아웃하면 저장소의 모든 파일과 폴더가 작업 디렉토리에 다운로드된다. 그러나 대규모 프로젝트의 경우 모든 파일을 체크아웃할 필요가 없는 경우가 많다. 이럴 때 sparse checkout을 사용하여 특정 파일이나 폴더만을 선택적으로 체크아웃하여 작업 디렉토리에 가져올 수 있다. 이를 통해 로컬 저장소의 용량을 절약하고 작업 디렉토리를 정리할 수 있다.

 


Dart 공부를 하면서 하나의 레포지토리 안에 폴더 형식으로 실습 예제들을 관리하고 있다.
필요한 예제 코드만 clone 하기 위해 sparse checkout 기능을 사용해보았다.
 

내가 필요한 폴더

 
 
 
* 현재 git version 2.39.3
  

 

1. clone 받을 폴더 만들기 또는 이동

clone 받을 폴더를 만들거나 이미 만들어져 있다면 해당 폴더로 이동한다.
 

// 폴더 생성
// mkdir sample
mkdir [폴더 이름]


// 해당 폴더로 이동
// cd sample
cd [폴더 이름]

 

 

2. git 초기화

 

git init

 

 
 

3. clone 할 원격 저장소 추가

 

 

git remote add origin [remote 저장소 주소]
// git remote add origin https://github.com/~~~

 
 

4. 체크아웃 초기화

 

이 명령어는 작업 디렉토리(여기서는 'sample')를 sparse checkout 모드로 설정하고, 원격 저장소에서 선택한 파일이나 디렉토리만을 추출해서 해당 디렉토리에 가져올 수 있도록 한다.
 

git sparse-checkout init

 

 

그런데,
 

Git 공식 문서

 
git document에서 "git sparse-checkout init" 명령어가 특정 경로를 지정하지 않고 동작하는 set 명령어와 유사하게 작동하기 때문에 향후에 제거될 수 있다고 한다. (그러니까 "git sparse-checkout set" 이렇게 사용하는 것과 유사하다는 것)
실제로도 init 명령어 없이 바로 set 명령어를 실행했을 때도 정상적으로 파일이 checkout 되었다.
 

현재는 init을 사용해도 문제 없이 진행된다.


 

5. 체크아웃할 파일/폴더 경로 작성

 

git sparse-checkout set "A/B"
// git sparse-checkout set "lib/240304/"

 
 

6. pull 받기

 

git pull origin master

 

 

 

 

파일을 실행해 보면 가져온 내용만 들어있는 것을 확인할 수 있다.

 

 

 

git push

 

수정이 생겼다면, 기존 push 방법으로 push와 PR도 가능하다.

 

 


 
 
 
 
[참고]
https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository

 

How do I clone a subdirectory only of a Git repository?

I have my Git repository which, at the root, has two subdirectories: /finisht /static When this was in SVN, /finisht was checked out in one place, while /static was checked out elsewhere, like so:...

stackoverflow.com

 
https://git-scm.com/docs/git-sparse-checkout

 

Git - git-sparse-checkout Documentation

If your repository contains one or more submodules, then submodules are populated based on interactions with the git submodule command. Specifically, git submodule init -- will ensure the submodule at is present, while git submodule deinit [-f] -- will rem

git-scm.com



 
 

 

728x90