【问题标题】:Simple-git requesting username and password on each git commandSimple-git 在每个 git 命令上请求用户名和密码
【发布时间】:2019-03-08 11:08:37
【问题描述】:

这是https://www.npmjs.com/package/simple-git 的问题。我正在使用 npm 模块 simple-git 与本地 git-repository 一起工作。本地 git 存储库已经在本地结帐,我可以通过 git-bash 使用存储库(提交/推送/拉取所有工作)。 同时,通过 simple-git 在同一存储库上执行的任何命令都要求我每次都输入用户名和密码。

我怎样才能让 simple-git 使用我现有的凭据,并停止在每个 git 命令上询问我的用户名和密码?

PS 初始化 simple-git 时,我只需提供存储库的路径。这应该足够了,它应该使用 .git/config 文件。也许这个 .git/config 中的某些内容需要 / 可以设置以帮助它停止询问用户名和密码。

【问题讨论】:

标签: git git-credential-manager


【解决方案1】:

recommended way 指向带有凭据的存储库clone(因此远程已经包含用户名和密码)。

如果存储库已经被克隆并且您不想弄乱远程,您可以将带有凭据的 URL 传递给 git 命令。这是push 的示例。

const remotes = await git.getRemotes(true);
if (remotes.length) { // Otherwise it's a local repository, no push
    let remote = remotes[0].name; 
    if (remotes[0].refs.push.indexOf("@") < 0) { // credentials aren't in the remote ref
        remote = remotes[0].refs.push.replace("://", `://${USER}:${PASSWORD}@`);
    }
    const pushRes = await git.push(remote, "master");
}

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2015-06-28
    • 2012-11-08
    • 1970-01-01
    • 2018-10-21
    • 2018-07-05
    • 2022-01-08
    • 1970-01-01
    • 2012-06-13
    相关资源
    最近更新 更多