1

I have a GIT repository and inside several SUBMODULES.

The problem is every time someone updates submodule, I try to update my files by calling:

$ git submodule update

But what happens is it deletes all my uncommited files which is very annoying.
I tried to commit and push the files first but it doesn't let me push:

error: failed to push some refs to 'ssh://dev.ancreative.co.uk/var/git/library/Blocks.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast-forward'
section of 'git push --help' for details.*

So I try to call git submodule update, but then this deletion happens.

Anyone has any idea what can cause this problem? Thanks a lot.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
pdolinaj
  • 1,087
  • 13
  • 21

1 Answers1

1

git submodule update won't help you solve the "non-fast-forward" situation.
It will only checkout the ref that the parent repo references.
It won't pull anything.

Plus, as mentioned in the git book:

git submodule update checks out a specific commit, rather than the tip of a branch.
It's like checking out a tag: the head is detached, so you're not working on a branch.

$ git branch
* (no branch)
master

If you want to make a change within a submodule and you have a detached head, then you should create or checkout a branch, make your changes, publish the change within the submodule, and then update the superproject to reference the new commit:

$ git checkout master

So the fact that an update deletes your files (even if committed first) might be related to the fact you are in a detached branch. (Note: you can still get back your commits made in that detached branch after the 'git submodule update' erased everything by looking at the reflog)

Once you are in a proper branch within a submodule, you can:

  • try to push
  • if it fails because of a non-fast-forward situation, you can git pull first to merge with the latest remote commits.
  • then push again
  • finally, get back in the parent repo and commit there, to register the new SHA1 of your submodule.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250