2

这是问题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 中的某些内容需要 / 可以设置以帮助它停止询问用户名和密码。

4

1 回答 1

0

推荐的方法是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");
}
于 2019-05-30T14:34:06.777 回答