DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Shrink a Bloated Git Repository and Optimize Pack Files
  • Developer Git Commit Hygiene
  • A Comprehensive Guide to GitHub
  • Mastering Git

Trending

  • Seven Failure Modes Every HTTP Client Should Handle: Lessons From Fixing 31 Bugs
  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • Testing Asynchronous Systems, Part 4: A Technique-by-Level Test Strategy
  • Idempotent Consumers: Preventing Stale Retries With Version-Fenced Writes

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Keeping Submodules Up to Date in Git

Keeping Submodules Up to Date in Git

When your project gets complicated, it makes sense to split it up into manageable chunks. Git supports this process using submodules.

By 
Tim Myerscough
·
Dec. 05, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

When your project gets complicated, it makes sense to split it up into manageable chunks. Git supports this process using submodules.

When you create a reference to a submodule within your repository, you actually include a reference to a specific commit. If the repository you are referencing is updated, those changes aren’t automatically reflected in your repository. This sort of makes sense, since git has no idea whether the submodule you are referencing is under your control or not.

By default, the submodule is checked out in a detached head state. This is fine if you want a read-only copy, but a bit of a pain if you want to commit changes. In addition, by default, submodules aren’t updated when you pull changes to your repository meaning you can get out of sync.

Working with submodules on a specific branch, where you want to commit changes to the submodule, requires a slight change to your CLI commands. Because you are no longer working with one repository, but multiple repositories.

Creating Your Submodule

Create your submodule, pointing it to the destination branch

git submodule add -b <branch> <repository_url> <submodule_folder>

Track the Remote Branch

Once you have your submodule, and you are checked out on a branch, you need to ensure that it is configured to track the remote branch of the submodule

cd <submodule_folder>
git branch --set-upstream-to=origin/<branch>

Now you are tracking the remote branch. You can make changes to the sub-module, and push them to the remote repository as required.

Updating Your Repository (Including Submodules)

When you make changes to your submodule, then you need to commit the change in the parent to ensure that the pointer is kept up to date. However, this results in two commits - one to the submodule and one to the parent, and this may not always be desirable at the same time.

In order to ensure the submodules are up to date, when pulling changes, use the following commands:

cd <parent_repository>
git pull
git submodule foreach git pull

This will ensure that the repository and submodules are kept up to date on the appropriate branch.

Git Repository (version control)

Opinions expressed by DZone contributors are their own.

Related

  • Shrink a Bloated Git Repository and Optimize Pack Files
  • Developer Git Commit Hygiene
  • A Comprehensive Guide to GitHub
  • Mastering Git

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook