Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: how to push submodule to a remote repository?

Tags:

I use git to track website I'm working on. I work on my machine and push commits to a remote server configured following this guide: using Git to manage a website.

Last week I tried using Git submodules to manage some third party libraries and today I tried pushing to the server, finding out that in the server all the submodule directories are empty.

I tried adding and commiting changes to the local submodule, indeed git status says that the working directory is clean.

What can I do?

like image 970
Carlo Avatar asked Dec 04 '11 01:12

Carlo


People also ask

How do you push a submodule to a remote?

push third-party library to its central repository. add submodule in parent repo (make it aware of the new commit) and commit. push parent project to its central repo. parent's central repo hook checks out to your server, and updates submodule there.

How do I add a submodule to a repository?

In order to add a Git submodule, use the “git submodule add” command and specify the URL of the Git remote repository to be included as a submodule. When adding a Git submodule, your submodule will be staged. As a consequence, you will need to commit your submodule by using the “git commit” command.

How do I push to a remote repository?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.


1 Answers

The point of submodules is that they are git repositories within repositories, and the parent repo only knows what commit should be checked out in the submodule - it knows nothing about the content. So a server only aware of the parent project, which hasn't populated the submodules, will naturally see nothing in them.

You'll need to at some point initialize the submodules on your server. It looks like you've got a setup with your work tree separate from your repo, so just like with that git checkout -f, you'll need to accommodate that: GIT_WORK_TREE=/path/to/whatever git submodule update --init. Afterwards, when your hook runs git checkout -f after pushing, it'll also need to run git submodule update (again with the work tree appropriately set).

But it's more complicated than this. You haven't given any information about where your submodules came from, but a submodule is aware of its origin, just like your repository is. When you initialize one, it tries to clone from that origin, and updating it often requires fetching from that origin. If as I suspect, the origin for your third-party libraries is something public that you don't have push access to, you're going to have to set up your own central repositories for the submodules. When you commit in one of the submodules, you'd push to its central repo, and then push the parent project, so that when it tries to update submodules elsewhere, it's able to fetch them.

So, to recap, the workflow is something like this:

  • commit in third-party submodule (or standalone clone of it)
  • push third-party library to its central repository
  • add submodule in parent repo (make it aware of the new commit) and commit
  • push parent project to its central repo
  • parent's central repo hook checks out to your server, and updates submodule there
like image 141
Cascabel Avatar answered Sep 18 '22 09:09

Cascabel