서브모듈 추가 및 복제
프로젝트에 새 서브모듈을 추가하는 명령과 서브모듈이 포함된 저장소를 올바르게 복제하는 방법을 학습합니다.
서브모듈 추가 및 복제은(는) CoddyKit의 무료 Git Advanced: Monorepo, Submodules & Workflows 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Git Advanced: Monorepo, Submodules & Workflows 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Git Advanced: Monorepo, Submodules & Workflows 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Add & Clone Git Submodules
Git submodules let you embed one Git repository inside another. This is useful for managing external dependencies like libraries or shared components.
In this lesson, we'll learn how to add a new submodule to your project and how to properly clone a repository that contains submodules.
The `git submodule add` Command
To add a submodule, you use the git submodule add command. This command takes the URL of the external repository and optionally a local path where you want to place it.
- Repository URL: The URL (HTTPS or SSH) of the submodule's Git repository.
- Path: The directory within your main project where the submodule will reside. If omitted, Git uses the repository name.
Add Submodule in Action
Let's say you have a my-project repository and want to include a shared library from https://github.com/example/shared-lib.git.
First, navigate to your main project's directory.
cd my-project
git submodule add https://github.com/example/shared-lib.git lib/shared-libFiles Created by `submodule add`
After running git submodule add, Git does a few things:
- It clones the submodule repository into the specified path (e.g.,
lib/shared-lib). - It adds an entry to a special file called
.gitmodulesin your main repository's root. - It stages the new submodule directory and the
.gitmodulesfile for commit.
Inspecting `.gitmodules`
The .gitmodules file is a configuration file that stores the mapping between the submodule's local path and its remote URL. It's crucial for others to clone your project correctly.
Here's what it might look like for our example:
[submodule "lib/shared-lib"]
path = lib/shared-lib
url = https://github.com/example/shared-lib.gitCommit Your Submodule Change
Adding a submodule is a change to your main repository, so you need to commit it. The commit will record the specific commit hash of the submodule's repository that your main project is currently using.
git status
git commit -m "Add shared-lib as a submodule"Cloning a Parent Repository
When you clone a repository that contains submodules, Git clones the parent repository but doesn't automatically download the submodule content.
The submodule directories will appear empty or contain only a .git file pointing to the submodule's repository.
The Easy Way to Clone All
The most straightforward way to clone a repository and all its submodules is to use the --recurse-submodules flag with git clone.
This command clones the main repository and then automatically initializes and updates all submodules to their correct versions.
git clone --recurse-submodules https://github.com/your/my-project.gitManually Initializing Submodules
If you forget --recurse-submodules during cloning, don't worry! You can initialize and update them manually after cloning the parent repository.
First, navigate into the cloned parent repository.
cd my-project
git submodule initFetching Submodule Content
After initializing, you need to tell Git to fetch the actual content for each submodule. This is done with the git submodule update command.
It fetches the data from the submodule's remote and checks out the specific commit recorded by the parent repository.
cd my-project
git submodule updateCheck Your Submodule Knowledge
You've just cloned a repository named parent-repo which is known to contain a submodule. After cloning with just git clone parent-repo.git, what are the necessary commands to get the submodule's content ready for use?
Recap: Add & Clone Submodules
In this lesson, you learned how to:
- Add a submodule using
git submodule add <url> <path>, which creates a.gitmodulesfile and stages the submodule. - Commit submodule changes to record the specific version of the submodule in the parent repository.
- Clone a repository with submodules using
git clone --recurse-submodulesfor an all-in-one operation. - Manually initialize and update submodules with
git submodule initandgit submodule updateif you cloned without the recurse flag.
Next, we'll explore how to manage and update submodules after they've been added and cloned.
자주 묻는 질문
“서브모듈 추가 및 복제” 강의는 무료인가요?
네 — “서브모듈 추가 및 복제” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Git Advanced: Monorepo, Submodules & Workflows 강의 전체를 잠금 해제할 수 있습니다. Git Advanced: Monorepo, Submodules & Workflows 강의에는 총 4개의 강의가 포함되어 있습니다.
“서브모듈 추가 및 복제”에서 뭘 배우나요?
프로젝트에 새 서브모듈을 추가하는 명령과 서브모듈이 포함된 저장소를 올바르게 복제하는 방법을 학습합니다. 브라우저에서 직접 실행하는 실습 코드로 Git Advanced: Monorepo, Submodules & Workflows을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Git Advanced: Monorepo, Submodules & Workflows을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Git Advanced: Monorepo, Submodules & Workflows은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“서브모듈 추가 및 복제” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Git Advanced: Monorepo, Submodules & Workflows 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Git Advanced: Monorepo, Submodules & Workflows 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Git 서브모듈 소개
- 서브모듈 추가 및 복제
- 서브모듈 업데이트 및 동기화
- 서브모듈 제거 및 초기화 해제