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).
Use the git submodule update command to set the submodules to the commit specified by the main repository. This means that if you pull in new changes into the submodules, you need to create a new commit in your main repository in order to track the updates of the nested submodules.
Checkout From Specific Git Commit ID Follow the steps to checkout from a specific commit id. Step 1: Clone the repository or fetch all the latest changes and commits. Step 2: Get the commit ID (SHA) that you want to checkout. From your local repository, you can get the commit SHA from the log.
You can set the submodule to track a particular branch (requires git 1.8. 2+), which is what we are doing with Komodo, or you can reference a particular repository commit (the later requires updating the main repository whenever you want to pull in new changes from the module – i.e. updating the commit hash reference).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With