0

I have trouble understanding how submodules work.

I have a remote repository PARENT and a remote repository CHILD. I would like to make repository CHILD a submodule of repository PARENT, so when I check out PARENT, CHILD will be checked out too (into a subfolder of PARENT). So my first try was setting up PARENT as a bare repository. But when I wanted to add the submodule:

[git ~/repositories/PARENT] git submodule add git://github.com/CHILD.git CHILD
fatal: /usr/libexec/git-core/git-submodule cannot be used without a working tree.

So I made PARENT a non-bare repository with a proper working tree (even though I really don't need a working tree in that repository). Adding the submodule went fine. Cloning PARENT into a local repository went fine also, and CHILD was checked out as a subfolder, just as intended. Then, when I wanted to push my changes from my local repository back to PARENT again:

branch is currently checked out
error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error: 
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error: 
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

So I can't use submodules in my bare repository, because it has a working tree, but when I use a non-bare repository, I can't check in, because it has a working tree? What am I not understanding here?

Jesper Rønn-Jensen
  • 91,561
  • 40
  • 112
  • 147
  • 1
    Your issue isn't with `submodules`, it's an inability to push to a non-bare repository (which happens both with and without submodules). Generally, a `bare` repository is a `remote` (you don't add directly into the `bare` repo, you add a submodule to your *local* repo, then push the changes (thus pushing your submodule into the `remote` repo as a commit)). – simont Apr 14 '13 at 12:42
  • Wow, that was easy. Thank you, simont. – Amelie Mentor Apr 14 '13 at 12:57
  • Maybe add that as an answer instead, so it can be accepted? – JosefAssad Apr 15 '13 at 10:55

1 Answers1

2

Your issue isn't with submodules, it's caused by an attempt to push to a non-bare repository (whether you're pushing a commit containing a submodule or not).

A bare repository is a remote (you don't work in the repository directly). To add a submodule to the remote repository, you add a submodule to your local repo, then git push the changes to the remote (thus pushing your submodule into the remote repo as a commit).

simont
  • 57,012
  • 16
  • 110
  • 130