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

问题描述 投票:0回答:3

我必须自动化克隆存储库并获取其所有子模块。存储库子模块的 URL 在

.gitmodules
处指定。如果我要使用默认值,我就会这样做

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

问题是凭据未包含在

.gitmodules
文件中,当我克隆时,系统会提示我输入这些凭据。我必须使用 HTTPS 而不是 SSH。

我尝试使用 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

但在最后一个更新步骤中系统提示我输入凭据。我已检查子模块 URL 是否正确,并且在

.git/config
文件中具有正确的凭据。

git git-submodules git-clone git-config
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 不包括用户名?.


8
投票

编辑

.gitmodules
文件后,您需要使用

应用更改
git submodule sync

下次运行时

git submodule update
将使用新的网址。


0
投票

如果您像我一样正在运行 CI 并有权访问私有令牌并且已经知道子模块的路径,则可以使用

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

下一个子模块更新将不会要求凭据。

显然,完成后您会想要清理它。就我而言,我在 docker 容器中运行,所以我没有这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.