![]() ![]() Choose a relative path where to include the common submodule, e.g.Perform the following steps for each parent repository (client and server) respectively:.Note: if you’re not starting from scratch but want to move existing files from a parent repository into the common submodule, then read this article. Fill the common repository, so that it has at least a master branch with one commit.If you’re testing things locally, just create an empty directory on your file system and use git init -bare to initialize it. Repositories you create on platforms like GitHub or Gitlab are such bare repositories. Create a bare git repository for the common submodule.For instance, a Java source file must be in folder a/b/c if its content starts with package a.b.c. Beware that some programming languages have rules regarding the file contents and the file’s directory structure. server) and the submodule ( common) is seamless and unnoticed! When the submodule is integrated into the parent repositories, the directory structure needs to “make sense”, in all parent repositories. your IDE) the transition between the parent repository (e.g. Git submodules are similar to Linux mount points. If you still want to use Git submodules, think of a sensible directory structure, as shown in the image above.In case you have access to private repositories/registries of such mechanisms, you could just push your versioned common module to those, and let these tools handle the dependencies, rather than using Git submodules. tools like Gradle (Java), pip (Python) or NPM (JavaScript). There might be better alternatives for managing dependencies, e.g. Discuss with your teammates whether using submodules actually makes sense. ![]() Also practice the other steps (adopting submodules, etc., see below). If you are doing this for the very first time, I recommend you practice these steps on a toy project. The steps are oriented towards the client/server/common example I introduced above. The following steps illustrate how to create a submodule, reference it from a parent repository, and commit and push the changes. Creating a submodule (for the first time) These are creating a submodule, adopting a submodule, and changing and committing code. Let’s examine the three main tasks which are either new, or have changed, because you now use submodules. ![]() Git allows you to commit this difference, as a new commit, to the parent repository. Whenever the commit currently checked out in the src/common repository is different than the one contained in the virtual src/common file, Git will detect this as a difference. From the perspective of the parent repository, Git pretends that the mount point path, such as src/common, is a virtual file, whose content is the commit hash of the submodule that should be checked out. This information is part of the regular commits of the parent repository. gitmodules file contains information regarding which submodules there are, and where to clone them from, it does not contain the specific commit of the submodule that should be checked out by the parent repository. gitmodules file is a simple text file, committed to each parent repository as regular file, even though it starts with “.git”. the content of folder src/common is a regular Git repository (with a. For each submodule, it contains the relative “mount point” path in the parent repository (which is src/common in this case), and the Git remote URL, which the Git binary needs, for it to know where to clone the submodule from. gitmodules file states which submodules the parent repository has. The two “parent” repositories named client and server both reference the repository named common as submodule. Let’s consider the following graphical illustration of the example I presented in the Introduction section. In this article I’ll try to make the concepts and commands as clear as possible, and also provide a few tips specific to the Git interface of JetBrains IDEs (e.g. While Git submodules provide a powerful mechanism to structure your repositories and avoid code duplication, there are caveats! Submodules make checking out and committing code more complex and error-prone. ![]()
0 Comments
Leave a Reply. |