2

I have a Git submodule that I have successfully cloned in to my main project. However, when I make changes in the submodule and try to push them back to the original submodule repository, Git refuses to do it. Why, and how can I fix this?

Example: I have project sub and project super in two separate folders. I included sub as a submodule under super. I make changes in folder super/sub, then try to push these changes to the original project sub. Git complains.

EDIT: Regarding the error message, I'm using Git Bash on Windows and unfortunately cannot get the exact error message copied out of the terminal. Here's the most pertinent lines:

remote: error: refusing to update checked out branch: refs/heads/master. By default, updating the current branch in a non-bare repository is denied, because it will make the index and work tree inconsistent with what you pushed, and will require 'git reset --hard' to match the work tree to HEAD.

Note: Both repositories live on my hard drive.

0

1 Answer 1

4

This has nothing to do with the repo being a submodule.

You are pushing to a git repo with a working copy (a non-bare repo), and are trying to push to the same branch that is currently checked out - by default git doesn't let you do that.

Before continuing you should ask yourself if you really want to do that. That's similar to, let's say you cloning a colleagues repo directly and then trying to push to it. If you succeeded, it's possible that you could cause your colleague to loose work if they had uncommitted changes, or local files which when updated will be overwritten with tracked files.

Easy solution

The easiest solution is to not push at all and just pull from your other repo instead. The end result is the same, but since your're pulling updates in (and that's simply normal) git won't say anything about that.

Slightly less-easy solution

You can push into your other working copy, if you change the receiving repo's git config (read the rest of the error message) to allow it to be pushed into. To do that, in the receiving repo:

git config receive.denyCurrentBranch ignore

More details about that are in the help (git config --help)

receive.denyCurrentBranch
    If set to true or "refuse", git-receive-pack will deny a ref update to the
    currently checked out branch of a non-bare repository. Such a push is potentially
    dangerous because it brings the HEAD out of sync with the index and working tree.
    If set to "warn", print a warning of such a push to stderr, but allow the push to
    proceed. If set to false or "ignore", allow such pushes with no message. Defaults
    to "refuse".
1
  • Thanks! That makes a lot of sense and I get what's going on now.
    – astay13
    Commented May 25, 2012 at 13:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.