Search code examples
git-submodules

Git Submodule : Cannot update submodule to an earlier revision


I have spent a lot of time in reading and trying out git submodules. But I have really given up.

My problem:

I am cloning a git repository as a submodule. So, I do

git submodule add -b master url localpath

The repository starts downloading and a corresponding entry is made in the .gitmodules file. git submodule status shows the latest commit.

So far so good.

During the development, it was found out that the HEAD commit in the submodule has a bug. So, the commit should be changed from master to some commit id.

So, I changed the commit id ( 7 aplhanumeric ) in the .gitmodule file from branch = master

branch = <commitid> -> .gitmodule file

And did

git submodule update --remote

The error is

Fatal: Need a single revision

Unable to find current origin/ revision in submodule path

I also tried

 git submodule foreach git pull

so that it will pull the commit id, but it simply returns with

Entering name_of_the_submodule

Already up to date.

Can anyone please help me?

P.S: I have already gone through many tutorials and question and answers. So if you want to downvote this for fun, then please first point to an answer and I will delete this post.


Solution

  • After you have cloned the submodule as described in your question:

    git submodule add -b master url localpath
    

    Then you should be able to changedir to the submodule:

    cd localpath
    

    Once in the submodule, you can use any git commands and they will only affect the submodule's repo. For example, you can checkout that holy branch or revision:

    git checkout <branch|tag|hash>
    

    or checkout the second-to-last revision in master branch:

    git checkout HEAD~1
    

    Then changedir back to the main git repo:

    cd ..
    

    And git status should yield something like:

    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   .gitmodules
        new file:   localpath
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   localpath (new commits)
    

    Then just add the changes done to your submodule:

    git add localpath
    

    And commit

    git commit -m "Add submodule url to localpath with older working commit"