In previous article, I introduced a way to manage project using sub-module. This time, I want to talk about cloning git repository with sub-module.
To clone the repository that has sub-modules, simply open git bash and navigate to the folder that the cloned file should be. If custom-folder-name is not specified, the folder name would be same as remote repository.
git clone --recurse-submodules <repository url> <custom-folder-name>
Github desktop provides clone function as well but It does not have custom-folder-name option. Folder name should be edited manually in this case.
Running above command line make log like below.
It clone the main codes first and clone codes in each sub-modules. And checkout to each commit-hash
not branch
.
But what is checkout
anyway?
Checkout is command to update and align (local) git with target. In this case commit-hash
. When git repository that has sub-module is committed, each sub-module is committed as current commit, not branch.
Each sub-module might have update while the main repository is committed. The main repository can not track sub-modules all the time. So it freeze sub-module as current commit and incorporate them in the main repository.
So cloning process checkout each sub-module to commit-hash
that the last commit of the main repository recorded.
Although above process do checkout and update already, If the sub-module has any update below command could be useful.
git submodule update --init --recursive
The real problem begins here.
As I mentioned each sub-module checked out to commit-hash
which means any update on each sub-module would be recorded on the commit-hash
.
Any updates are supposed to be tracked by branch, so in this state, update would be lost. Since git has no idea where the change should be recorded.
Therefore all we need to do is to specify what branch each sub-module would use.
- Use the current branch.
- Create the new branch to record from now.
To use current branch, navigate to where sub-module is and checkout or switch to the branch. (The switch command is new so that, It seems many editors that supports git still show command as checkout)
cd path/to/submodule
[OLD] git checkout main # or the appropriate branch name
[NEW] git switch main # or the appropriate branch name
Let’s see in more detailed way with another sub-module.
The branch before checkout is at HEAD detached at commit-hash
and there are two branches currently. What I did above section is move to main branch
from commit-hash
.
So If we want to use new branch to track from now on?
Do same command with additional option as below.
[OLD] git checkout -b <new-branch-name> # -b means branch
[NEW] git switch -c <new-branch-name> # -c means create
In conclusion, It is important to check the current status of git and update branch properly not to lose your important works!