Will Nelson
Will Nelson

Reputation: 976

Git submodules: A confusing statement

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?

Upvotes: 1

Views: 211

Answers (3)

abnvp
abnvp

Reputation: 1027

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

Upvotes: 1

plasmixs
plasmixs

Reputation: 156

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

Upvotes: 0

redhotvengeance
redhotvengeance

Reputation: 27866

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.

Upvotes: 3

Related Questions