Git basics

Add and update Git submodules

Keep one Git project inside another, fetch it after cloning and bring it up to date.

Fill these in once
  1. Add another project inside this one
    Terminal
    git submodule add https://github.com/user/library.git libs/library
  2. Save it
    Terminal
    git commit -m "Add libs/library submodule"
  3. Cloned a project that has submodules? Fetch them
    Terminal
    git submodule update --init --recursive

    Their folders are empty until you run this.

  4. Update a submodule to its latest version
    Terminal
    git submodule update --remote libs/library

    Then commit, so everyone gets the same version.

Check it worked

  1. List the submodules
    Terminal
    git submodule status
Undo it
  1. Remove a submodule
    Terminal
    git rm libs/library

    Then commit the change.

  1. Add another project inside this one
    Terminal · Ubuntu / Debian
    git submodule add https://github.com/user/library.git libs/library
  2. Save it
    Terminal · Ubuntu / Debian
    git commit -m "Add libs/library submodule"
  3. Cloned a project that has submodules? Fetch them
    Terminal · Ubuntu / Debian
    git submodule update --init --recursive

    Their folders are empty until you run this.

  4. Update a submodule to its latest version
    Terminal · Ubuntu / Debian
    git submodule update --remote libs/library

    Then commit, so everyone gets the same version.

Check it worked

  1. List the submodules
    Terminal · Ubuntu / Debian
    git submodule status
Undo it
  1. Remove a submodule
    Terminal · Ubuntu / Debian
    git rm libs/library

    Then commit the change.

  1. Add another project inside this one
    PowerShell
    git submodule add https://github.com/user/library.git libs/library
  2. Save it
    PowerShell
    git commit -m "Add libs/library submodule"
  3. Cloned a project that has submodules? Fetch them
    PowerShell
    git submodule update --init --recursive

    Their folders are empty until you run this.

  4. Update a submodule to its latest version
    PowerShell
    git submodule update --remote libs/library

    Then commit, so everyone gets the same version.

Check it worked

  1. List the submodules
    PowerShell
    git submodule status
Undo it
  1. Remove a submodule
    PowerShell
    git rm libs/library

    Then commit the change.