← Back to Git Course | Chapter 6: Advanced Commands | Lesson 1 of 7

Git Submodules

एक submodule अपने binder के अंदर एक पूरी और book रखने जैसा है, एक particular page पर freeze की गई। आपका project उस outside project को उसका owner बने बिना इस्तेमाल करता है।
Syntax
bash
git submodule add repository_url path
git clone --recurse-submodules repository_url
git submodule update --init --recursive

एक Submodule Add करना

एक submodule किसी दूसरी Git repository को आपके project की एक subdirectory की तरह embed करता है, एक branch track करने के बजाय एक specific commit पर pinned। यह किसी separate shared library की exact history पर बिना उसकी commits को अपनी repository में merge किए depend करने का standard तरीका है।

उदाहरण: Adding a Submodule

bash
git submodule add https://github.com/user/library.git libs/library

Submodules वाली Repositories Clone करना

जब आप submodules वाला एक project git clone करते हैं, Git submodule directories बनाता है लेकिन उन्हें खाली छोड़ता है — parent repo सिर्फ एक pointer store करता है कि हर submodule किस commit पर होना चाहिए। उन्हें actually populate करने के लिए आपको git submodule init और git submodule update चलाना ज़रूरी है (या --recurse-submodules से clone करना)।

उदाहरण: Cloning Repositories with Submodules

bash
git clone --recurse-submodules https://github.com/user/repo.git
git submodule update --init --recursive

Submodule Code Update करना

git submodule update --remote हर submodule के अपने remote से latest commits fetch करता है और आपके local checkout को match करने के लिए आगे move करता है, लेकिन यह automatically parent repository के pinned pointer को update नहीं करता — आपको अभी भी खुद वह pointer change git add और commit करनी होगी।

उदाहरण: Updating Submodule Code

bash
git submodule update --remote
git add libs/library
git commit -m "Update submodule"

Submodule Commands चलाना

git submodule foreach '<command>' हर registered submodule directory के अंदर sequence में एक arbitrary shell command चलाता है, आपको manually हर एक में cd करने से बचाते हुए — सबमें एक साथ git status चलाने जैसी चीज़ के लिए उपयोगी।

उदाहरण: Running Submodule Commands

bash
git submodule foreach 'git status'

एक Submodule Delete करना

एक submodule को cleanly हटाने के लिए सिर्फ इसका folder delete करने से ज़्यादा चाहिए: आपको इसकी entry .gitmodules और .git/config से हटानी होगी, फिर इसे untrack करने के लिए git rm --cached <path> चलाना होगा, और आख़िर में leftover .git/modules/<path> cache directory delete करनी होगी — इनमें से कोई भी step skip करना stale references पीछे छोड़ देता है।

उदाहरण: Deleting a Submodule

bash
git rm --cached libs/library
rm -rf .git/modules/libs/library
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Submodules वाली एक repository clone करना और खाली folders मिलना, जबकि आपको git submodule update --init --recursive या --recurse-submodules चाहिए।
  2. एक submodule के अंदर changes commit करना और parent project में updated pointer commit करना भूल जाना।
  3. एक detached HEAD पर रहते हुए submodule files edit करना और commits खो देना, जबकि आपको पहले एक branch checkout करना चाहिए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.