Clone a repo with submodules: override credentials

回眸只為那壹抹淺笑 提交于 2021-01-02 05:31:50

问题


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.


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/30040643/clone-a-repo-with-submodules-override-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!