0Pricing
Git Advanced: Monorepo, Submodules & Workflows · Leçon

Mise à jour et synchronisation des sous-modules

Maîtrisez la mise à jour des sous-modules vers leurs dernières versions et la synchronisation de leurs URL.

Mise à jour et synchronisation des sous-modules est une leçon Git Advanced: Monorepo, Submodules & Workflows gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Git Advanced: Monorepo, Submodules & Workflows, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Git Advanced: Monorepo, Submodules & Workflows comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

Keeping Submodules Current

When you work with Git submodules, the main project (superproject) usually points to a specific commit of the submodule, not necessarily its latest version.

This lesson teaches you how to update your submodules to newer versions and ensure their configurations are correct.

The Basic `git submodule update`

The most common command to update submodules is git submodule update. This command does two main things:

  • It checks out the exact commit that the superproject expects for each submodule.
  • It ensures the submodule's working directory matches this recorded commit.

It does not fetch new commits from the submodule's remote repository.

git submodule update

Updating Recorded Commits

Imagine your main project's .gitmodules file specifies a submodule my-lib. When someone else updates my-lib in its own repository and then updates the superproject's pointer to my-lib, you'll need to update.

After pulling changes in the superproject, run this to get the submodule's files to match the new recorded commit:

git pull origin main
git submodule update

Updating to Latest with `--remote`

What if you want your submodule to point to the absolute latest commit on its remote tracking branch, not just the one recorded by the superproject?

That's where the --remote option comes in handy. It fetches from the submodule's remote and updates its HEAD to the tip of its configured tracking branch (e.g., main or master).

`--remote` in Practice

Using --remote is powerful for quickly bringing submodules up-to-date with their own upstream changes. It's like doing a git pull inside each submodule.

Remember, after doing this, your superproject's submodule pointer will be 'out of sync'. You'll need to commit the submodule's new SHA-1 hash to the superproject.

git submodule update --remote
git add my-lib
git commit -m "Update my-lib submodule to latest"

What is `git submodule sync`?

Sometimes, the URL for a submodule's remote repository might change. When this happens, the URL stored in your local Git configuration (.git/config) for that submodule needs to be updated.

The git submodule sync command is designed to synchronize these URLs. It reads the URLs from the .gitmodules file and updates the local configuration for each submodule.

When to Use `sync`

You'll typically use git submodule sync in these situations:

  • After someone changes a submodule's URL in the .gitmodules file and you pull those changes.
  • When you clone a repository with submodules for the first time, though git submodule update --init often handles this implicitly.
  • If you manually changed a submodule's URL in .gitmodules and want your local config to reflect it.

`sync` in Action

Let's say the URL for your my-lib submodule changed from old-url.git to new-url.git in the .gitmodules file. After pulling that change into your superproject, your local configuration for my-lib is still pointing to the old URL.

Run this command to fix it:

git submodule sync my-lib
# Or for all submodules:
git submodule sync

A Comprehensive Update Workflow

For a robust workflow that covers most scenarios when updating submodules, consider combining commands:

  • git pull origin main: Get latest superproject changes.
  • git submodule sync: Update submodule URLs if they changed.
  • git submodule update --init --recursive --remote: Initialize any new submodules, update all existing ones to their latest remote, and handle nested submodules.

This ensures you're fully up-to-date.

git pull origin main
git submodule sync
git submodule update --init --recursive --remote

Submodule Update Challenge

You've pulled a new version of your superproject. Your colleague informs you they've updated a submodule called shared-utils in its own repository, but they haven't yet committed the new submodule pointer to the superproject.

Which command should you use to get the shared-utils submodule to its latest state from its remote, without waiting for the superproject to record it?

Recap: Keeping Submodules Fresh

You've learned how to manage submodule updates:

  • git submodule update: Checks out the commit recorded by the superproject.
  • git submodule update --remote: Fetches the latest changes from the submodule's remote and updates its working directory.
  • git submodule sync: Synchronizes submodule URLs from .gitmodules to your local Git config.

These commands are essential for maintaining a healthy and up-to-date project with submodules!

Questions Fréquemment Posées

La leçon « Mise à jour et synchronisation des sous-modules » est-elle gratuite ?

Oui — le texte complet de « Mise à jour et synchronisation des sous-modules » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Git Advanced: Monorepo, Submodules & Workflows, passe à CoddyKit PRO. Le cours Git Advanced: Monorepo, Submodules & Workflows comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Mise à jour et synchronisation des sous-modules » ?

Maîtrisez la mise à jour des sous-modules vers leurs dernières versions et la synchronisation de leurs URL. Tu pratiques Git Advanced: Monorepo, Submodules & Workflows avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Git Advanced: Monorepo, Submodules & Workflows ?

Aucune expérience préalable n'est requise. Git Advanced: Monorepo, Submodules & Workflows sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.

Combien de temps prend la leçon « Mise à jour et synchronisation des sous-modules » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Git Advanced: Monorepo, Submodules & Workflows ?

Oui. Chaque leçon Git Advanced: Monorepo, Submodules & Workflows inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Introduction aux sous-modules Git
  2. Ajout et clonage de sous-modules
  3. Mise à jour et synchronisation des sous-modules
  4. Supprimer et désinitialiser des sous-modules
← Retour à Git Advanced: Monorepo, Submodules & Workflows