Friday, November 30, 2018

How to use a Git Submodule's https url without username

We were starting on a new project and we had a git repository for it (no surprises here :)). This repository was hosted on bitbucket. We were using it over HTTPS because of various reasons.

The Problem

Soon enough we wanted to add a submodule to it. This submodule was hosted on bitbucket as well. This posted a tricky situation, because the HTTPS url looks like

"https://deep@bitbucket.com/git/module.git"

Notice that it embeds the username i.e. deep into the URL itself. This URL is used in the .gitmodules file that is tracked in the parent repository.

This wouldn't have been a problem if, I was the only one working on the project. However, there were other developers wanting to work on this repository and check out the submodules. Unless I get rid of the username from the submodule URL, other developers would not be able to pull/push the submodules and contribute to the repository.

The Solution

I looked around and finally found a very simple solution to this problem. The solution works very well, as long as the main repository and submodules reside on the same server i.e. bitbucket in our case. Lets say that the parent repository URL was 

"https://deep@bitbucket.com/git/parent.git"
  • As you can see both the parent and the submodule are hosted on the same server.
  • We need to edit the .gitmodules file located at the root of the parent repository. This is a text file and can be opened in any text editor.
  • Locate the Submodule and its HTTPS URL there. It would look something like this
  • Next, we need to edit the submodule URL and make it relative to the parent repository URL
  • In this case, the relative url would look like "../module.git"
  • Updated .gitmodules looks like this
  • After this all we need to do is push the updated .gitmodules.
  • Thats it, now other developers will be able to pull and push changes to the parent repository and its submodules like any other repository!


Have some Fun!