1

I have a GitHub repo that contains two submodules. I made several changes to these submodules and committed them. However, it seems I have not done this correctly.

I recently lost my local copy of the repository, and am trying to clone the remote. However, when I run

git clone --recursive myrepo.git

I get the error:

fatal: reference is not a tree: ...
Unable to checkout ... in submodule path....
Unable to checkout ... in submodule path....

When I click the submodule link the GitHub, I get a 404 error.

I have two questions.

  1. It seems it is not possible to restore these submodules with the commits I have made to them. Is this true?

  2. If #1 is true. What is the best way to pull back in the current upstream submodules (considering the error above)? I can reproduce the commits I made to the submodules manually fairly easily.

DJElbow
  • 3,345
  • 11
  • 41
  • 52

1 Answers1

1

It seems it is not possible to restore these submodules with the commits I have made to them. Is this true?

Yes, because you committed the submodule content, but did not push them to their respective remote repos.

If #1 is true. What is the best way to pull back in the current upstream submodules (considering the error above)?

You need to checkout the submodule gitlink, the special entry in the index (meaning their folder name) at at previous state, in order for those gitlinks to reference a SHA1 which is present in their remote repo.
Start by listing their history

git log -- yourSubmodule

Pick a previous commit, and type:

git show <previouscommit> -- yourSubmodule

You will see to which SHA1 that gitlink entry refers to.

Once you checkout that gitlink to a previous commit, a git submodule update --init should restore the content of the older SHA1 from that gitlink, allowing you to cd into that submodule folder and restore the commits you did before.
Don't forget to add, commit and push from within that submodule, before going back into the parent repo, add commit and push there as well.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Great answer. Thank you. When I try to push the submodule, it is trying to push to the original repo, not my remote. Do you have a link on how to do this as well? Having a hard time finding an answer to this. – DJElbow Mar 31 '16 at 02:23
  • @DJElbow the submodule repo is supposed to push to its own repo, not your original forked repo. What a git remote -v returns when executed inside that submodule? The same as when executed in your parent local repo? If so, it is *not* a submodule. Just a regular subfolder. – VonC Mar 31 '16 at 07:04