Andy Shinn
Andy Shinn

Reputation: 28523

How to properly update a git submodule?

I know this question has been beaten to death. But I still don't have a clear understanding of why my submodule is trying to refer to a commit in my superproject. I have a project which has a number of submodules, some of which reference github repositories which I want to contribute to. After updating the module, I pull the changes of the module:

[ashinn@puppet1 puppet]$ cd modules/ganglia
[ashinn@puppet1 ganglia]$ git branch
* (no branch)
  master
[ashinn@puppet1 ganglia]$ git pull origin master
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 4), reused 4 (delta 2)
Unpacking objects: 100% (6/6), done.
From https://github.com/andyshinn/puppet-ganglia
 * branch            master     -> FETCH_HEAD
Merge made by recursive.
 README |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

Now I want to update the reference to the module in the superproject:

[ashinn@puppet1 ganglia]$ cd ../..
[ashinn@puppet1 puppet]$ git add modules/ganglia
[ashinn@puppet1 puppet]$ git commit -m 'updated ganglia module'
[ganglia c172591] updated ganglia module
 1 files changed, 1 insertions(+), 1 deletions(-)

I am currently working on the ganglia branch, so I push it to my origin (which is 2 differen't URLs):

[ashinn@puppet1 puppet]$ git push origin ganglia
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 616 bytes, done.
Total 6 (delta 4), reused 0 (delta 0)
To [email protected]:andyshinn/puppet.git
   1876698..c172591  ganglia -> ganglia
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 323 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: From /var/lib/puppet/repo
remote:    dcd1fcc..c172591  ganglia    -> origin/ganglia
remote: From https://github.com/andyshinn/puppet-ganglia
remote:    a0c4e21..975c92f  master     -> origin/master
remote: fatal: reference is not a tree: c5defdeae006c7b87058cc5c79aef60087b63a6b
remote: Unable to checkout 'c5defdeae006c7b87058cc5c79aef60087b63a6b' in submodule path 'modules/ganglia'
remote: Updating existing environment ganglia
To [email protected]:repo
   dcd1fcc..c172591  ganglia -> ganglia

The remote has a post-receive script to checkout branches in different folders and run git submodule update --init for each. If I manually connect to the origin repo I can verify the issue:

-bash-4.1$ git pull
Already up-to-date.
-bash-4.1$ git submodule update --init
fatal: reference is not a tree: c5defdeae006c7b87058cc5c79aef60087b63a6b
Unable to checkout 'c5defdeae006c7b87058cc5c79aef60087b63a6b' in submodule path 'modules/ganglia'

What am I doing wrong here?

Upvotes: 1

Views: 1422

Answers (1)

Adam Dymitruk
Adam Dymitruk

Reputation: 129654

You were not on the branch when you did the pull. HEAD must be pointing to where you want it to be. The proper way to update your submodule would be to

git checkout master
git push origin master

now the current commit points to where you want it to be. Once you do this, you can add the submodule change in the super project, add, commit and push.

The other alternative is to make a branch or tag of where your current HEAD in the submodule is and push that up:

git tag interesting HEAD
git push origin interesting
cd ../..
git add submodule/path
git commit -m "updated my submodule"
git push 

Upvotes: 0

Related Questions