15

I have to automate cloning a repository and fetching it's all submodules. The urls for repository submodules are specified at .gitmodules. If I were to go with defaults I would just do

git clone --recursive https://username:password@url.git

The problem is credentials aren't included in .gitmodules file and I am prompted for those when I clone. I have to use HTTPS rather then SSH.

I tried to submit the credentials using git config:

git clone https://username:password@url.git my_repo
cd my_repo
git submodule init
git config submodule.my_submodule.url "https://username:password@url/my_submodule.git"
git submodule update

but I get prompted for credentials in the last update step. I've checked that the submodule url is correct and has proper credentials in .git/config file.

sakovias
  • 1,356
  • 1
  • 17
  • 26

3 Answers3

8

Looks like you are trying to use git credentials but not having any luck.

Option 1

Add credentials using the credential helper:

git config credential.https://example.com.username myusername
git config credential.helper "$helper $options"

Check your ~/.gitconfig and verify that the appropriate entry is added.

Further reading: http://git-scm.com/docs/gitcredentials

Option 2

I would double check the contents of your .git-credentials file and make a new entry for the submodule if it is not present. This file is used internally by the git credentials helper.

http://git-scm.com/docs/git-credential-store

Option 3

Easy solution in windows is to remove the username, password from modules file:

[submodule foo]
  path = sub/foo
  url = https://example.com/git/foo.git

And create a ~/.netrc file.

machine example.com
  login myusername
  password areamandyingtotellsomeonehiscoolpassword

Kudos to Git submodule URL not including username?.

Community
  • 1
  • 1
jaypb
  • 1,544
  • 10
  • 23
  • Note that when you use setup a creadential helper you might need to provide a path to a file explicitly. Git assumes $HOME variable is set and that is not true e.g. in Jenkins. – Nux Jan 18 '22 at 15:50
  • So a basic usage would be: `git config --global credential.helper store` But you might need to use: `git config --system credential.helper 'store --file /root/.git-credentials'` – Nux Jan 18 '22 at 15:51
8

After editting the .gitmodules file you need to apply changes using

git submodule sync

The next time you run git submodule update the new url will be used.

Sébastien Dawans
  • 4,537
  • 1
  • 20
  • 29
0

If, like me, you are running CI and have access to a private token and already know the path to the submodule, you could use

git config credential.helper store
echo "https://LOGIN:${PAT}@github.com/path/to/submodule.git" > ~/.git-credentials

The next submodule update will not ask for credential.

Obviously, you'll want to clean this up once your done. In my case, I run in a docker container, so I don't have this concern.

Norman Pellet
  • 374
  • 2
  • 9