1

The page http://git-scm.com/book/en/Git-Tools-Submodules in the Pro Git book discusses creation of a submodule as a directory within a git project. This is the command used to add the submodule:

$ git submodule add git://github.com/chneukirchen/rack.git rack

Later in the page, it says

You can treat the rack directory as a separate project and then update your superproject from time to time with a pointer to the latest commit in that subproject.

What does this mean? I don't know what "pointer" or "update" mean in this context. Where and how is this pointer stored? How does one update the super project?

Will Nelson
  • 966
  • 8
  • 13
  • Where is it stored? In `.gitmodules`, but also in the index (for the SHA1 of that submodule repo) as a special entry: http://stackoverflow.com/a/2227598/6309. What does it mean? See http://stackoverflow.com/q/1979167/6309. – VonC Oct 17 '13 at 06:32

3 Answers3

3

A submodule is a standalone Git repo - what makes it a submodule is how it is referenced by the "super project", or rather, the Git repo that the submodule repo is sitting inside of.

When you make a change to a repo that is being utilized as a submodule, you'll be working as you normally do with a Git repo - you'll make your changes, add them, commit them, and push them. But after you make those changes in the submodule repo, you'll notice in the "super project" repo that there will be uncommitted changes, and those changes will reference the submodule repo.

So what has changed? Well, a "super project" repo keeps tabs on the submodule repo - its location, name, and (this is important) its current commit. Why the commit? Because it allows you to specify in the "super project" what is the proper commit to reference in the submodule. This has many advantages - picture using a third-party lib (as a submodule) that has recently been recently updated with backwards-breaking changes that you have not yet updated your project to accommodate for. By keeping the reference to an older commit, you can ensure that anyone who starts working on the project will be using the correctly-supported version of the submodule.

If you want to update the "super project" to reference the latest commit of the submodule, simply add, commit, and push as you would any other changed file.

redhotvengeance
  • 27,446
  • 10
  • 49
  • 54
1

Dev1 & Dev2 have the below source code structure.

  • SrcTree
    • MainCode
    • SubModule

Dev2 makes some changes to SubModule and pushes the changes.

  • from the subModule
    • "git add submodule's files"
    • "git commit"
    • "git push"
  • from the SrcTree
    • add
    • commit
    • push // this is going to update the pointer of the submodule

Dev1 needs to do the below actions to correctly update the subModule

  • git pull // this will change the pointer but not update the submodule as mentioned here
  • git submodule update // this will update the submodule
abnvp
  • 1,037
  • 11
  • 19
  • `git submodule update` will pull the correct commit for a submodule, but it will not update the "super project" repo to point at the latest commit, as OP asked how to do. Your answer is missing the step where Dev1 has updated the reference to the new commit of the submodule in the "SrcTree" project. – redhotvengeance Oct 17 '13 at 02:08
  • Your update makes things more clear, but still misses a key part (in relation to OP's question). Specifically, in addition to pushing changes to the submodule, Dev2 will also need to commit the updated submodule commit to SrcTree and push that as well. Otherwise, when Dev1 does a `git pull`, the submodule "pointer" will not have been updated, and there will be nothing new to pull, nor will there be any update when running `git submodule update`. – redhotvengeance Oct 17 '13 at 04:43
  • Very close. After doing the add/commit/push from the submodule, Dev2 needs to do an add/commit/push from the super project repo (SrcTree). *That* is the commit that will update to the pointer to the new submodule commit. – redhotvengeance Oct 17 '13 at 15:21
0

You can refer to this post about git submodules. http://plasmixs.wordpress.com/2013/10/06/git-submodules/

plasmixs
  • 156
  • 5