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

  • Git option not available under Version Control in Intellij
  • How to merge “develop” branch from different people in git flow branching model?
  • Git workflow for maintaining a derivative fork
  • filter a `git grep` search on more than one pattern
  • Giving access to parts of a github repository
  • Which version control system or platform is the best one for tracking and distributing personal Emacs configurations?
  • Managing multiple repositories with git - inspect changes and record state
  • git hook syntax check
  • Trying to get started with git
  • git bash questions
  • I can init and fetch file with git-svn, but can't clone
  • How do you prevent Xcode from auto selecting the Team Profile?
  • 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.

    Git Baby is a git and github fan, let's start git clone.