Update a git submodule

February 10th, 2014

Recently a colleague and I have been taking over a project that uses git submodules. When I first started work I cloned the project using the --recursive flag to also checkout the submodules. I didn't have any experience with submodules, so I ran into some problems, specifically updating and replacing modules. Here's what I have learned.

Updating a module

If you need to make changes to a submodule and have commit access to the submodule's repository, you just make the changes and commit them. The submodule is a git project, even though it does not have the traditional folders and files.

Then in the root project (where the .gitmodules file is located) you will see the following when running git status.

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   puppet/modules/php (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")

The (new commits) message means that there is a difference between what the root project in pointing to and what is actually present in the submodule folder. Now you need to commit the changes like normal, BUT when you add the submodule you can not write a / at the end. As far as I understand it, it's because you are referencing the label in quotes in .gitmodules (or in .git/config) and not an actual folder.

git add puppet/modules/php
git commit -m "Updated php submodule"

Replace submodule

If you do not have write access to the submodule repository, then you can fork it (if it's on github, or copy it otherwise if not) and replace the reference.

To replace a submodule you remove the folder, clone the new repository and updates the reference.

cd puppet/modules
rm -rf php
git clone git://github.com/troelsselch/puppet-php.git php
cd ../..
nano .gitmodules
# Change the url for the given module in your editor and save the changes.
git submodule update # to update .git/config

That should be it. Here are some Stack Overflow threads that helped me:

Categories

IT

Tags