A git submodule is a speciific folder or adress within a repository or porject that directs the contents of another respository into the specified file location. Submodules are overlooked by the git machine unless the submodule is entered. When looking at changes throught github desktop, changes made inside the module will not be detected and must be handled through the console.
The main component behind a git submodule is the `.gitmodules` file that is can either be auto-generated or manually created. The contents of this file provide the necessary information sumodule setup in the project, but further steps are required to setup code usage.
The contents of the .gitmodules:
[submodule "libs/3128-common"]
path = libs/3128-common
url = https://github.com/Team3128/3128-common.git
Submodules can be accessed and configured using the git submodule commands in the terminal.
To remove submodule, directory, and related chache:
git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>…]
git rm -r --cached libs/3128-common
To add the submodule from the github cloud to the libs/3128-common destination:
git submodule [--quiet] add [-b "branch"] [-f|--force] [--name "name"] [--reference "repository"] [--] "repository" ["path"]
git submodule add https://github.com/Team3128/3128-common.git libs/3128-common
--force may be appended if required.
In order for porjects in the main repository to acess files from the submodules, the directory must be included in the gradle proejct, and it must be configured as an implementation.
Navigate to the settings.gradle file in the main proejct and append the following inclusion and project:
include ':libs:3128-common'
project(':libs:3128-common').projectDir = file('libs/3128-common')
Navigate to the build.gradle file in the main project and add the project as a dependency:
dependencies {
implementation project(':libs:3128-common')
// other dependencies
}
Then cd into the submodules and build the gradle project. Starting from the main proejct:
cd libs/3128-common
./gradlew build
After, cd back to the main project and run a gralde build:
cd ../..
./gradlew build
- Remove any existing directories or chache
git rm -r --cached libs/3128-common
- Clone from default branch or specified branch:
git submodule add https://github.com/Team3128/3128-common.git libs/3128-common
- Include the project by navigating to
settings.gradleand adding:
include ':libs:3128-common'
project(':libs:3128-common').projectDir = file('libs/3128-common')
- Add the dependency by navigating to
build.graldeand adding:
dependencies {
implementation project(':libs:3128-common')
// other dependencies
}
- Build the submodule project:
cd libs/3128-common
./gradlew build
- Build the main project:
cd ../..
./gradlew build
The following is taken from the git help page.
To use these commands on a submodule, the
git submodule foreachcan be appended to the front to call for all modules, or the command can be called by navigating to the submodule directory.
To start a working area:
git cloneClone a repository into a new directorygit initCreate an empty Git repository or reinitialize an existing one
To work on the current change
git addAdd file contents to the indexgit mvMove or rename a file, a directory, or a symlinkgit restoreRestory working tree filesgit rmRemove files from the working tree and from the index
To examine the history and state:
git statusShow the working tree statusgit diffShow changes between commits, commit and working tree, ect.git bisectUse binary search to fine the commit that introduced a buggit grepPrint lines matching a patterngit logShow commit logsgit showShow various types of objects
To grow, mark, and tweak your common history:
git branchList, create, or delete branchesgit checkoutSwitch branches or restore working tree filesgit commitRecord changes to the repositorygit mergeJoin two or more development histories togethergit rebaseReapply commits on top of another base tipgit resetReset current HEAD to the specified stategit revertRevert some existing commitsgit stashStach the changes in a dirty working directory awaygit switchSwitch branchesgit submoduleInitialize, update, or inspect submodulesgit tagCreate, list, delete, or verify a tag object signed with GPG
To collaborate:
git fetchDownlead objects and refs from another repositorygit pullFetch from and integrate with another repository or local branchgit pushUpdate remote refs along with associated objects
Call git help -a to read all git commands.
./gradlew buildBuild gradle project for compile time errorsgit pullFetch from and integrate with origingit branch -aView all local and remote branchesgit checkout <BRANCH NAME>Switch branches with working tree filesgit stausShow the working tree stausgit add -Aorgit add <FILE PATH>Stage all untracked files or may specify certain filesgit statusShow the working tree status with all untracked filesgit branchDouble check current branchgit commit -m <COMMIT MESSAGE>Commit staged changes to current local branchgit pushPush commit changes from local to origin
git commit -am "<Commit Message>" Adds all untracked files and adds a commit message
git commit -um "<Commit Message>" Adds all tracked files and adds a commit message
If git submodule is present without any contents, run git submodule init and git submodule update
chmod +x nar.sh
./nar.sh submodule