0

Here is my project structure

MainProject
|  README.md
|  src/
|  lib/      # this is a submodule

When I develop MainProject, I will also develop lib/, I mean MainProject/lib/.

When I do push, I will copy all changes in MainProject/lib/* into MainProject/../lib/, which is a separate git directory, then push.

After that I will do a submodule update in MainProject/lib/ to latest from remote.

Then I will commit and push changes in MainProject.

It suffers. How do you develop along with a git submodule?

ADD

I want all changes in lib/ pushed to remote lib repository. And all changes beside lib/ in MainProject pushed to remote MainProject.

So if I directly put to remote lib repository inside MainProject/lib And then add and commit and push changes beside lib/ in MainProject to remote.

If someone else clone MainProject and do submodule update, will he get the latest sources?

My .gitmodules:

 [submodule "lib"]
    path = lib
    url = git@github.com:XXX/YYY.git
    branch = master
Sato
  • 8,192
  • 17
  • 60
  • 115

1 Answers1

1

You shouldn't have to copy your changes: you can push directly from MainProject/lib to the same upstream repo (the one in git remote origin) than the upstream repo (git remote origin) found in MainProject/../lib.

MainProject/lib is a nested repo in its own right: you can add commit and push them directly from it.

But then, you have to go back to MainProject/, add, commit and push in order to record the new gitlink (the special entry in the index of the parent repo which records the SHA1 of the submodule lib)


add and commit and push changes beside lib/ in MainProject to remote

No: when you commit anyhting in lib, that will change the gitlink (the 'lib' entry in MainProject index)

By "going back to MainProject" (meaning, you were in MainProject/lib and you do cd ..), you can do a git add ., which will:

  • add all your changes beside lib/
  • record the new SHA1 associated with lib/ submodule (the 'lib' gitlink entry)

But pushing the MainProject, complete with the updated gitlink 'lib' entry, you are making sure that anyone cloning your MainProject will get back lib at that exact updated SHA1.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. What do you mean "back to MainProject/, add, commit and push..."? I have added something to my post. Please read and help. Thanks. – Sato Aug 29 '14 at 06:22
  • @Sato I have edited my answer. It is important to realize how 'lib' is viewed from the perspective of its parent repo `MainProject`: as a gitlink entry in the index, in order to record the exact SHA1 at which the submodule is checked out. If you create a new commit in the submodule, you need to push it, but also go back to the main project and add/commit that gitlink entry. – VonC Aug 29 '14 at 06:49
  • So when others clone `MyProject` and do a `submodule update`, it will get the exact SHA1(gitlink entry recorded in `MainProject`) not just HEAD? – Sato Aug 29 '14 at 07:19
  • @Sato yes, a submodule *always* checks out at a fixed SHA1. Then, you can configure a submodule to update itself the HEAD of a specific branch. See my answers at http://stackoverflow.com/a/9189815/6309 and http://stackoverflow.com/a/18799234/6309 – VonC Aug 29 '14 at 07:25