Programming/Git Submodules: Difference between revisions

From Dev Wiki
Jump to navigation Jump to search
(Create page)
 
m (Brodriguez moved page Git Submodules to Programming/Git Submodules)
 
(No difference)

Latest revision as of 17:42, 25 October 2020

Git submodules are essentially git projects that are meant to be used within other projects.

For example, maybe you created a personal library that's meant to be a helper project for other, larger projects. Or maybe you're using someone else's library project, and the programming language doesn't really have a good way to distribute and download projects.


Installing Submodules

Submodules can be imported into another project with the command:

git submodule add <git_clone_url>

Note that the above command will add the submodule directly to the current project root. To specify a subfolder, use:

git submodule add <git_clone_url> <relative_path>

In either case, after adding the project, you'll need to use the standard git add and git commit commands.


Initializing Submodules

The above commands simply tell your git project "okay, I want you to be able to download this repo on my command, using the current master branch. And this is the location you download it to." That ensures that anyone who uses your project in the future is able to access the same project, at the same location, with the same code downloaded.

To actually download the submodule, you can do one of two ways: 1) When you git clone your repo, add the --recurse-submodules flag. Ex:

git clone <git_clone_url> --recurse-submodules

This will download any submodules currently defined in the project's master branch, at time of cloning.

2) If the project is already cloned, or the submodules aren't in the master branch, then select the correct branch and run:

git submodule update --init --recursive


Updating Submodules

To update the current version of code each submodule points to, you can use the command:

git submodule foreach git pull origin master

This will check each submodule added to the project, examine each corresponding master branch, and pull changes if there are any.

Once again, to save these changes, you'll need to use the standard git add and git commit commands.