Add submodule with specific tag not branch in one command
Asked Answered
D

2

7

For a rails template I'd like to add a submodule of a specific tag to new rails apps. To keep this simple I'd like to avoid going into subdirectories and running git commands there.

git submodule add --branch v1.3.37 [email protected]:foo.git vendor/foo

Is what I would like to use, but it does not accept tags for the --branch parameter:

fatal: 'origin/v1.3.37' is not a commit and a branch 'v1.3.37 cannot be created from it Unable to checkout submodule 'vendor/foo'

Is there a simple way to add a git submodule on a specific tag?

Dobb answered 30/6, 2017 at 22:2 Comment(0)
G
6

I don't think it is possible.

This is the submodule command line reference.

git submodule [--quiet] add [<options>] [--] <repository> [<path>]
add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--depth <depth>] [--] <repository> [<path>]

As you can see, right now it only supports branch option, and in terms of Git objects, the difference between branch and tag is discussed in here reference:

branch

A "branch" is an active line of development. The most recent commit on a branch is referred to as the tip of that branch.

tag

A ref pointing to a tag or commit object. In contrast to a head, a tag is not changed by a commit.

So, until Git team supports SHA commit checkout for submodule, you cannot checkout specific tag.

Gayn answered 2/7, 2017 at 11:0 Comment(0)
P
0

You can make a workaround: just fork target repository and create a new branch from upstream tag.

git clone [email protected]:foo.git /tmp/foo && cd /tmp/foo
git checkout -b release-v1 v1
git push --set-upstream origin release-v1
cd ~/workspace && git submodule add --branch release-v1 [email protected]:foo.git vendor/foo

But later you will have to create new branches from news tags before using it as git submodule.

Pelagias answered 20/4, 2021 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.