How can I set a submodule to point to a specific commit without fetching it?
I am writing a service that updates the commit that each submodule in a superproject points to. My naive way of doing this would be to run
git fetch in a submodule,
git reset --hard <hash>, and then add the submodule and commit it.
I would like to skip the
git fetch step and simply force the submodule to point to a given hash for better performance (skip fetching the objects and taking up disk space) and to handle commits that may no longer exist upstream and can’t be fetched anyway (if they were clobbered by a force push).
One Solution collect form web for “How can I set a submodule to point to a specific commit without fetching it?”
The solution is to write to the Git index directly, which actually is simple for submodules. With Git 2:
git update-index --cacheinfo 160000,<Git hash of the submodule's tree>,<path to the submodule>
So for example if your project directory structure looks like this:
. ├── .git └── submodule
Then to update the submodule to point to commit 2764a900748fbed7453f5839cb983503cee346d2 you would run:
git update-index --cacheinfo 160000,2764a900748fbed7453f5839cb983503cee346d1,submodule
And finally follow it up with
git commit as usual.