Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 993
ide Avatar asked Nov 04 '15 05:11

ide


People also ask

How do I make a submodule point to a specific commit?

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.

How do you checkout to a specific commit?

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.

Can a submodule point to a branch?

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).


1 Answers

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.

like image 144
ide Avatar answered Oct 24 '22 06:10

ide