Git: how to push submodule to a remote repository?

前端 未结 4 1362
梦谈多话
梦谈多话 2020-12-28 17:04

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.

4条回答
  •  臣服心动
    2020-12-28 17:32

    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

提交回复
热议问题