Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding git submodule and "freezing" it at a specific commit hash or version

Assuming the following project layout:-

mainrepo_git
    |____ .git
    |____ .gitmodules
    |____ proj            <------- directory containing the code files for mainrepo
            |____ 3rdpartysourcecode <-- directory containing upstream open source code
            |      |____ .git
            |      |____ 3rdpartyfiles
            |
            |____ mainrepofilesanddirectories  

mainrepo_git contains source code I am directly responsible for. I have read/write access and can push and pull directly to a remote git repository which I manage.

Nested inside mainrepo_git is a directory which I named 3rdpartysourcecode. This 3rdpartysourcecode directory is in fact another git repo (also commonly referred to as a "git submodule") which is pointing to an open source 3rd party git repository managed by other developers. I only have read access to it. No write access.

Is there any way of 'freezing' a specific commit hash of the git submodule in relation to a commit made in my main repository?

For example, if I am at (or I revert to) commit a12ucak in my mainrepo, my git submodule also gets reverted to a specific version which I tie to commit a12ucak? And when I switch to commit b349jdsak, my git submodule also gets reverted to a version which I tie to b349jdsak?

So my question is: there is a way to create a linkage between a specific commit in the main repo with a corresponding commit in the git submodule? In such a way where when I checkout that specific commit in the main git repo, the corresponding commit in the git submodule will also be checkout.

like image 874
Calvin Cheng Avatar asked Aug 30 '11 03:08

Calvin Cheng


People also ask

How does git store submodule commit?

It is stored in Git's object database directly. The tree object for the directory where the submodule lives will have an entry for the submodule's commit (this is the so-called "gitlink").

How does git submodule work?

A git submodule is a record within a host git repository that points to a specific commit in another external repository. Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated.

Is using git submodules a good idea?

In most cases, Git submodules are used when your project becomes more complex, and while your project depends on the main Git repository, you might want to keep their change history separate. Using the above as an example, the Room repository depends on the House repository, but they operate separately.

Does git pull pull submodules?

Pulling with submodules. Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .


1 Answers

Freezing is the whole point of submodules. You should really read a tutorial on it. In a nutshell, git add 3rdpartysourcecode (important no trailing slash!) followed by a commit locks the currently checked out submodule commit to the supermodule commit. Then later you use git submodule update to check out that revision.

like image 189
Karl Bielefeldt Avatar answered Sep 22 '22 12:09

Karl Bielefeldt