How would I go about adding a Git submodule for a specific tag or commit?
-
3Possible duplicate of [How can I specify a branch/tag when adding a Git submodule?](https://stackoverflow.com/questions/1777854/how-can-i-specify-a-branch-tag-when-adding-a-git-submodule) – Duncan Jones Dec 12 '18 at 09:18
2 Answers
Submodule repositories stay in a detached HEAD state pointing to a specific commit. Changing that commit simply involves checking out a different tag or commit then adding the change to the parent repository.
$ cd submodule
$ git checkout v2.0
Previous HEAD position was 5c1277e... bumped version to 2.0.5
HEAD is now at f0a0036... version 2.0
git-status
on the parent repository will now report a dirty tree:
# On branch dev [...]
#
# modified: submodule (new commits)
Add the submodule directory and commit to store the new pointer.

- 19,579
- 7
- 67
- 84
-
25After I commited the parent repo with a specific branch/version of a submodule, can others checkout the same version of submodule in the future? Or, they have to specified manually again? – QZHua Apr 07 '17 at 03:41
-
1@QZHua: I experimented with this myself and it seems that the commit ID of the submodule is preserved when the parent is cloned. – Psychonaut May 16 '19 at 13:46
-
1and here we go into the dark zone..... i can commit to my local git,.... but maybe not push to origin.... which could render this unsharable..... experience tells..... beware! if the submodule changes a lot it will cause trouble – U.V. Oct 22 '20 at 13:14
Step 1: Add the submodule
git submodule add git://some_repository.git some_repository
Step 2: Fix the submodule to a particular commit
By default the new submodule will be tracking HEAD of the master branch, but it will NOT be updated as you update your primary repository. In order to change the submodule to track a particular commit or different branch, change directory to the submodule folder and switch branches just like you would in a normal repository.
git checkout -b some_branch origin/some_branch
Now the submodule is fixed on the development branch instead of HEAD of master.
From Two Guys Arguing — Tie Git Submodules to a Particular Commit or Branch .

- 30,738
- 21
- 105
- 131

- 5,661
- 2
- 35
- 54
-
22It's easier to use `git submodule add -b some_branch git://some_repository.git some_repository` – Caumons Oct 02 '13 at 00:42
-
23At this moment it doesn't seem that `git submodule add -b` can take a tag or a sha1hash. It can only take a branch. – CMCDragonkai Apr 04 '16 at 14:33
-
Is there an issue for this in the Git issue tracker? Would someone kindly post a URL so we can track it? Thanks. – colan Nov 02 '18 at 20:04
-
@colan instructions for reporting bugs are at https://git-scm.com/community. But `-b`'s supporting branches and not tags or commit shas is a feature not a bug (in fact `-b` is shorthand for `--branch`). – henry Jul 08 '20 at 16:57
-
10Sigh. After all these years, they still don't have a public issue tracker. – colan Jul 09 '20 at 17:50
-
There's no github for git, but you can just search the archives: https://public-inbox.org/git/?q=submodule+add+tag – daraul Mar 04 '21 at 00:10