克隆带有子模块的仓库:覆盖凭据

16

我需要自动克隆一个代码库并获取其所有子模块。代码库子模块的URL在 .gitmodules 文件中指定。如果我使用默认设置,我会执行以下操作:

git clone --recursive https://username:password@url.git
问题在于.gitmodules文件中不包含凭据信息,当我克隆时会提示我输入凭据。我必须使用HTTPS而不是SSH。 我尝试使用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

但是在最后一个更新步骤中,我会被提示输入凭据。我已经检查了子模块的URL是否正确,并且在.git/config文件中有正确的凭据。


1
你尝试过使用“git submodule sync”命令吗? - Sébastien Dawans
1
事实证明,我没有使用正确的名称来命名 my_submodule。被 Tab 键自动完成误导了。我在 .gitmodules 中找到了正确的名称。 - sakovias
3个回答

8

看起来您正在尝试使用 Git 凭据,但没有成功。

选项1

使用凭据助手添加凭据:

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

请检查您的~/.gitconfig文件,并确保已添加相应的条目。
进一步阅读: http://git-scm.com/docs/gitcredentials 选项2: 如果子模块不存在,请仔细检查.git-credentials文件的内容并添加新条目。该文件是由git凭据助手在内部使用的。 http://git-scm.com/docs/git-credential-store 选项3: 在Windows上的简单解决方法是从模块文件中删除用户名和密码:
[submodule foo]
  path = sub/foo
  url = https://example.com/git/foo.git

同时创建一个名为 ~/.netrc 的文件。

machine example.com
  login myusername
  password areamandyingtotellsomeonehiscoolpassword

Git子模块URL不包括用户名的问题致敬。


请注意,当您设置凭据助手时,您可能需要明确提供文件路径。Git 假定 $HOME 变量已设置,但在 Jenkins 中并非如此。 - Nux
一个基本的用法是: git config --global credential.helper store但你可能需要使用: git config --system credential.helper 'store --file /root/.git-credentials' - Nux

8

在编辑.gitmodules文件后,您需要使用以下命令来应用更改:

git submodule sync

下次运行 git submodule update 时将会使用新的url。

0

如果像我一样,你正在运行CI并且有访问私有令牌的权限,已经知道子模块的路径,那么你可以使用以下代码:

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

下一个子模块更新将不会要求凭证。
显然,你应该在完成后清理它。在我的情况下,我在 Docker 容器中运行,所以我不必担心这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接