Git submodule head 'reference is not a tree' error Git submodule head 'reference is not a tree' error git git

Git submodule head 'reference is not a tree' error


Assuming the submodule's repository does contain a commit you want to use (unlike the commit that is referenced from the current state of the super-project), there are two ways to do it.

The first requires you to already know the commit from the submodule that you want to use. It works from the “inside, out” by directly adjusting the submodule then updating the super-project. The second works from the “outside, in” by finding the super-projects commit that modified the submodule and then resetting the super-project's index to refer to a different submodule commit.

Inside, Out

If you already know which commit you to want the submodule to use, cd to the submodule, check out the commit you want, then git add and git commit it back in the super-project.

Example:

$ git submodule updatefatal: reference is not a tree: e47c0a16d5909d8cb3db47c81896b8b885ae1556Unable to checkout 'e47c0a16d5909d8cb3db47c81896b8b885ae1556' in submodule path 'sub'

Oops, someone made a super-project commit that refers to an unpublished commit in the submodule sub. Somehow, we already know that we want the submodule to be at commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c. Go there and check it out directly.

Checkout in the Submodule

$ cd sub$ git checkout 5d5a3ee314476701a20f2c6ec4a53f88d651df6cNote: moving to '5d5a3ee314476701a20f2c6ec4a53f88d651df6c' which isn't a local branchIf you want to create a new branch from this checkout, you may do so(now or later) by using -b with the checkout command again. Example:  git checkout -b <new_branch_name>HEAD is now at 5d5a3ee... quux$ cd ..

Since we are checking out a commit, this produces a detached HEAD in the submodule. If you want to make sure that the submodule is using a branch, then use git checkout -b newbranch <commit> to create and checkout a branch at the commit or checkout the branch that you want (e.g. one with the desired commit at the tip).

Update the Super-project

Checkout in the submodule is reflected in the super-project as a change to the working tree. So we need to stage the change in the super-project's index and verify the results.

$ git add sub

Check the Results

$ git submodule update$ git diff$ git diff --cacheddiff --git c/sub i/subindex e47c0a1..5d5a3ee 160000--- c/sub+++ i/sub@@ -1 +1 @@-Subproject commit e47c0a16d5909d8cb3db47c81896b8b885ae1556+Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c

The submodule update was silent because the submodule is already at the specified commit. The first diff shows that the index and work tree are the same. The third diff shows that the only staged change is moving the sub submodule to a different commit.

Commit

git commit

This commits the fixed-up submodule entry.


Outside, In

If you are not sure which commit you should use from the submodule, you can look at the history in the superproject to guide you. You can also manage the reset directly from the super-project.

$ git submodule updatefatal: reference is not a tree: e47c0a16d5909d8cb3db47c81896b8b885ae1556Unable to checkout 'e47c0a16d5909d8cb3db47c81896b8b885ae1556' in submodule path 'sub'

This is the same situation as above. But this time we will focus on fixing it from the super-project instead of dipping it into the submodule.

Find the Super-project's Errant Commit

$ git log --oneline -p -- subce5d37c local change in subdiff --git a/sub b/subindex 5d5a3ee..e47c0a1 160000--- a/sub+++ b/sub@@ -1 +1 @@-Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c+Subproject commit e47c0a16d5909d8cb3db47c81896b8b885ae1556bca4663 added subdiff --git a/sub b/subnew file mode 160000index 0000000..5d5a3ee--- /dev/null+++ b/sub@@ -0,0 +1 @@+Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c

OK, it looks like it went bad in ce5d37c, so we will restore the submodule from its parent (ce5d37c~).

Alternatively, you can take the submodule's commit from the patch text (5d5a3ee314476701a20f2c6ec4a53f88d651df6c) and use the above “inside, out” process instead.

Checkout in the Super-project

$ git checkout ce5d37c~ -- sub

This resets the submodule entry for sub to what it was at commit ce5d37c~ in the super-project.

Update the Submodule

$ git submodule updateSubmodule path 'sub': checked out '5d5a3ee314476701a20f2c6ec4a53f88d651df6c'

The submodule update went OK (it indicates a detached HEAD).

Check the Results

$ git diff ce5d37c~ -- sub$ git diff$ git diff --cacheddiff --git c/sub i/subindex e47c0a1..5d5a3ee 160000--- c/sub+++ i/sub@@ -1 +1 @@-Subproject commit e47c0a16d5909d8cb3db47c81896b8b885ae1556+Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c

The first diff shows that sub is now the same in ce5d37c~. The second diff shows that the index and work tree are the same. The third diff shows the only staged change is moving the sub submodule to a different commit.

Commit

git commit

This commits the fixed-up submodule entry.


try this:

git submodule syncgit submodule update


This error can mean that a commit is missing in the submodule. That is, the repository (A) has a submodule (B). A wants to load B so that it is pointing to a certain commit (in B). If that commit is somehow missing, you'll get that error. Once possible cause: the reference to the commit was pushed in A, but the actual commit was not pushed from B. So I'd start there.

Less likely, there's a permissions problem, and the commit cannot be pulled (possible if you're using git+ssh).

Make sure the submodule paths look ok in .git/config and .gitmodules.

One last thing to try - inside the submodule directory: git reset HEAD --hard