首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >simple-git:使用simple的SSH身份验证示例

simple-git:使用simple的SSH身份验证示例
EN

Stack Overflow用户
提问于 2021-10-07 11:05:28
回答 1查看 514关注 0票数 0

如何使用SSH使用simple-git克隆存储库

我试着用这个代码克隆

代码语言:javascript
运行
复制
const git: SimpleGit = simpleGit();
const sshUri = 'git@..........'
const localPath = '/usr/workspace';
git
 .clone(
   sshUri,
   localPath
 ).then((data) => {
    console.log('success');
 }).catch((err) => {
     console.log(err);
 });

但我得到了一个例外

代码语言:javascript
运行
复制
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
EN

回答 1

Stack Overflow用户

发布于 2022-03-12 03:42:47

如果您的git回购是私有的,则需要生成SSH密钥并将此密钥添加到您的GIT帐户中。

生成SSH密钥的https://docs.github.com/en/authentication/connecting-to-github-with-ssh

  • Add

代码示例

代码语言:javascript
运行
复制
    private async cloneRepo() {

        const gitSSH = 'FILL YOUR SSH GIT ADDRESS';
        const sourceDir = path.resolve(`${os.tmpdir()}/${this.projectKey}`);
        const sshKnownHosts = path.resolve(`${process.cwd()}/settings/ssh/known_hosts`)
        const sshKey = path.resolve(`${process.cwd()}/settings/ssh/id_ed25519`)

        const GIT_SSH_COMMAND = `ssh -o UserKnownHostsFile=${sshKnownHosts} -o StrictHostKeyChecking=no -i ${sshKey}`;

        console.log(sourceDir);

        const git: SimpleGit = simpleGit()
            .env('GIT_SSH_COMMAND', GIT_SSH_COMMAND)
            .clean(CleanOptions.FORCE);

        await git.clone(gitSSH, sourceDir, ['-b', 'YOUR-BRANCH-NAME', '--single-branch'])
            .then(() => console.log('finished'))
            .catch((err) => console.error('failed: ', err));
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69480019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档