Clone a repo with submodules: override credentials
Asked Answered
C

3

17

I have to automate the cloning of a repository and fetching all its submodules. The URLs for the repository submodules are specified at .gitmodules. If I were to go with defaults I would just do

git clone --recursive https://username:[email protected]

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 than SSH.

I tried to submit the credentials using git config:

git clone https://username:[email protected] 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.

Consumerism answered 4/5, 2015 at 21:54 Comment(2)
did you try git submodule sync ?Barnet
Turns out I wasn't using proper name for my_submole. Got misled by tab completion. I found proper name in .gitmodules.Consumerism
M
9

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?.

Mayest answered 4/5, 2015 at 22:15 Comment(2)
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.Meerschaum
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'Meerschaum
E
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.

Eweneck answered 4/5, 2015 at 22:16 Comment(0)
M
2

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.

Mapel answered 17/9, 2020 at 14:12 Comment(1)
> 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 ------ how did you know I am like you?! Thanks for this, exactly what I was looking for :)Cloudcapped

© 2022 - 2024 — McMap. All rights reserved.