How to create Git submodule or commit object through gitlab API?

How to create Git submodule or commit object through gitlab API?

Let me give an example

Now there are two repositories in my gitlab server, namely main and sub1.I want to make sub1 a sub-module of main should do this:

git clone http://server/main.git
cd main
git submodule add http://server/sub1.git
git add *
git commit -m "add submodule"

Is there an API that allows me to send an HTTP request to achieve these operations.

Hi @igmainc

The docs for the submodules API are here and the docs for commit are here

@snim2
I have read the documentation, the API of the sub-module only provides update functions, and the commit API cannot be used to create the sub-module.

Right, so in that case I don’t think it is possible directly from the API.

You could write a script that clones the repo, adds the submodule like you would on the command-line and then pushes back to GitLab. The clone and push could be done via the API, but TBH I’d be inclined to just call git directly from a shell script or a library. Potentially you could do this from within your CI/CD pipeline, and just write it into your .gitlab-ci.yml file, if that makes sense in your workflow.

You’ll need a personal access token for this BTW.

1 Like