gitgit-submodulesgit-add

git add submodule hash directly


Is there a way using git commands to directly update the submodule hash in the index?

If not, is there a way to manually change it via modifying text somewhere in the .git directory?

I want to point to a specific commit in a submodule, but I want to avoid having to go into that submodule, check out the commit, add the submodule to the index, and then checking out the original commit in the submodule. For example:

  1. cd <submod>
  2. git checkout <hash>
  3. cd ..
  4. git add <submod>
  5. cd <submod>
  6. git checkout <originalbranch>
    • To determine <originalbranch> programattically may take several more commands.

Of course, this could all be scripted, but since the submodule state is captured in the index via a single hash, it should be possible to have a single command to do this without this long unnecessary flow of actually checking the commit out.


Solution

  • Collecting useful nuggets of information from several places which led me to find an easy one-liner solution that does exactly what the question asks:

    git update-index --cacheinfo 160000 <hash> <submod>
    

    Note that 160000 is the mode of submodule gitlinks. Not sure if it is required to include it, but I've confirmed that it works.