Categories
git git-submodules

Git submodule head ‘reference is not a tree’ error

330

I have a project with a submodule that is pointing to an invalid commit: the submodule commit remained local and when I try to fetch it from another repo I get:

$ git submodule update
fatal: reference is not a tree: 2d7cfbd09fc96c04c4c41148d44ed7778add6b43
Unable to checkout '2d7cfbd09fc96c04c4c41148d44ed7778add6b43' in submodule path 'mysubmodule'

I know what the submodule HEAD should be, is there any way I can change this locally, without pushing from the repo that does have commit 2d7cfbd09fc96c04c4c41148d44ed7778add6b43 ?

I’m not sure if I’m being clear… here’s a similar situation I found.

3

  • 12

    “fatal: reference is not a tree” in reference to submodules appears to generally mean the submodule commit that the parent repo expects has not yet been pushed, or is screwed up in some other way. For us this confusing error message was resolved by just pushing a submodule someone forgot to push.

    Jan 27, 2014 at 19:45

  • 1

    @ChrisMoschini – I just had that issue, and that was my “solution”, I pushed and pull the main repo., but I forgot to push my last commit to the submodule’s repo. Thanks!

    – Rotem

    May 10, 2016 at 13:24

  • Maybe you forgot to push the latest submodule commits

    Nov 20, 2016 at 2:58

394

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 update
fatal: reference is not a tree: e47c0a16d5909d8cb3db47c81896b8b885ae1556
Unable 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 5d5a3ee314476701a20f2c6ec4a53f88d651df6c
Note: moving to '5d5a3ee314476701a20f2c6ec4a53f88d651df6c' which isn't a local branch
If 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 --cached
diff --git c/sub i/sub
index 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 update
fatal: reference is not a tree: e47c0a16d5909d8cb3db47c81896b8b885ae1556
Unable 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 -- sub
ce5d37c local change in sub
diff --git a/sub b/sub
index 5d5a3ee..e47c0a1 160000
--- a/sub
+++ b/sub
@@ -1 +1 @@
-Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c
+Subproject commit e47c0a16d5909d8cb3db47c81896b8b885ae1556
bca4663 added sub
diff --git a/sub b/sub
new file mode 160000
index 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 update
Submodule 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 --cached
diff --git c/sub i/sub
index 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.

4

  • In the “Outside, In” approach, could you elucidate on why “it looks like it went bad in ce5d37c?” What fingers that one as the bad commit?

    Feb 18, 2011 at 21:53


  • 5

    @Garrett: The assumption is e47c0a is a commit that does not exist in the local repository for sub, yet the super-project’s sub points to that commit. This might have happened because someone else created e47c0a in their copy of sub, updated their super-project to point to that commit and pushed the super-project without pushing e47c0a to the central/shared repository for sub. When we pull from the central/shared super-project we get a commit that points sub to e47c0a, but we can not “see” that commit. ce5d37c is suspect because, based on the diff, it introduced e47c0a.

    Feb 18, 2011 at 23:35

  • It is still left rather vague where is the specific hash of the sub kept in the parent repo which has it as a submodule, and whether or not it can be manipulated directly to the current HEAD of sub directly, without relying on an older state of the parent repo, which may not always help.

    – matanster

    Oct 29, 2015 at 11:21


  • @matanster: It is stored in Git’s object database directly

    May 7, 2016 at 14:39

192

try this:

git submodule sync
git submodule update

5

  • 2

    Not for me unfortunately, one of our submodules was targeted by the main git repository with an add command, now having trouble undoing it

    – Daniel

    Feb 15, 2012 at 18:00

  • 9

    Worked for me too. Would love to know why though.

    – BenBtg

    Jul 20, 2012 at 13:42

  • 12

    Turns out that doing a git submodule sync is necessary in scenarios where the URL of the remote for a given submodule has changed. In our case we had added our submodule from a public repo and then changed the URL to a private fork – and got ourselves into this particular pickle.

    – Samscam

    Dec 20, 2012 at 16:44

  • For example: I had a repo (A) set up with a submodule pointing to my github repo (B). I created a branch in the repo A because I wanted to point B at someone else’s github repo. After a bit of struggling with that and committing the branch, I switched my repo A back to master and had this problem with repo B. @Lonre Wang’s solution fixed it.

    – fbicknel

    May 20, 2014 at 19:20

  • 2

    Assuming nobody REALLY screwed up (in which case you’d need Chris Johnsen excellent answer) the answer by Lonre Wang should fix the problem,… UNLESS your submodules have submodules of their own (and the problem is inside a submodule). In that case you need to cd into the submodule that has the submodule with the problem and execute the above commands. Note that update has a –recursive option (git submodule update –recursive), but sync doesn’t; you really have to manually run ‘git submodule sync’ inside the submodule that has the problematic sub(sub)module. This was my problem ;).

    Oct 11, 2016 at 23:21

18

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

4

  • 3

    I already explained that in the question… the question itself was how to solve it. And it has already been successfully answered almost two years ago… Permissions have nothing to do with this.

    Nov 15, 2011 at 22:08


  • 1

    You stated it, you certainly didn’t explain it.

    Nov 15, 2011 at 23:35

  • this helped me out: “inside the submodule directory: git reset HEAD –hard”

    – k2s

    Feb 15, 2012 at 15:08

  • 5

    the “git reset HEAD –hard” helped me too… nothing else worked. I tried the previous solutions too, no dice. Thanks!

    – Virgil

    Nov 23, 2012 at 15:40