git:git-submodules
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
git:git-submodules [2023/05/02 16:39] – admin | git:git-submodules [2024/12/12 08:44] (current) – admin | ||
---|---|---|---|
Line 2: | Line 2: | ||
If you clone a git repo inside you git, the best way to handle it is to use modules (sub modules). | If you clone a git repo inside you git, the best way to handle it is to use modules (sub modules). | ||
- | To define | + | |
+ | ==== Create a submodule ==== | ||
+ | |||
+ | To define | ||
<code bash> | <code bash> | ||
Line 15: | Line 18: | ||
git submodule add https:// | git submodule add https:// | ||
</ | </ | ||
+ | |||
+ | ==== List all submodules ==== | ||
We can list the submodules with : | We can list the submodules with : | ||
Line 22: | Line 27: | ||
Information on submodules can be found in **.gitmodules** and **.git/ | Information on submodules can be found in **.gitmodules** and **.git/ | ||
+ | |||
+ | ==== Content changes in submodules ==== | ||
If we add the changes, commit and push , the content of submodules will not been added in the main project git, but information on how to retrieve it will be stored. In our example we push the changes : | If we add the changes, commit and push , the content of submodules will not been added in the main project git, but information on how to retrieve it will be stored. In our example we push the changes : | ||
Line 30: | Line 37: | ||
</ | </ | ||
- | In another computer we can clone the main project repo : | + | ==== Clone git repo with submodules ==== |
+ | |||
+ | In another computer we can simply | ||
+ | <code bash> | ||
+ | git clone <git URL> | ||
+ | cd <git repo main folder> | ||
+ | git submodule update --init | ||
+ | </ | ||
+ | If we know when cloning the main project git that there are submodules we can pull them at the start with **--recursive** keyword: | ||
+ | <code bash> | ||
+ | git clone <git URL> --recursive | ||
+ | </ | ||
+ | |||
+ | if after pulling the main repo, some new submodules are created and if they are empty, their content can be retrieved with : | ||
+ | <code bash> | ||
+ | git submodule foreach git restore --staged . | ||
+ | git submodule foreach git restore . | ||
+ | </ | ||
+ | |||
+ | ==== Working with a particular submodule (not all) ==== | ||
+ | |||
+ | If the main git repo contains many submodules, it can be interesting to work only on one particular submodule. | ||
+ | For example, if after cloning the main git repo the submodule contains nothing, it can be cloned independently of the others submodules. | ||
+ | Suppose the path to submodule (from main repo path) is / | ||
+ | <code bash> | ||
+ | git submodule init / | ||
+ | git submodule update -- / | ||
+ | </ | ||
+ | If the submodule is not in the correct branch, these commands can be used to list the branches and set to the correct one (ex master) | ||
+ | <code bash> | ||
+ | git -C / | ||
+ | git -C / | ||
+ | </ | ||
+ | In general a git **command** on submodule can be acheived from main git repo by : | ||
+ | <code bash> | ||
+ | git -C / | ||
+ | </ | ||
+ | |||
git/git-submodules.1683038397.txt.gz · Last modified: 2023/05/02 16:39 by admin