gpt4 book ai didi

git - 如何在有和没有 ssh 身份验证的情况下将 Git 与两个不同的存储库一起使用?

转载 作者:行者123 更新时间:2023-12-02 14:13:59 24 4
gpt4 key购买 nike

我有一个问题:

我必须与项目合作:

  • 第一个在本地 gitlab 上,需要 ssh 身份验证
  • 第二个是在不需要 ssh 身份验证的简单 git 服务器上(没有 gitlab 或其他类似工具)

  • 一切都很好,当我使用 gitlab 的项目时,但是当我尝试 pull 、推、获取不在 gitlab 服务器上的项目时:git bash 返回:

    Enter passphrase for key '/c/Users/my_user/.ssh/id_rsa':



    但我不需要任何 ssh 身份验证!

    当我为 gitlab 项目运行 git remote -v 时,git bash 返回:

    $ git remote -v

    origin http://xxxx/yyyy-tools/yyyy-demat-purge.git (fetch)

    origin http://xxxx/yyyy-tools/yyyy-demat-purge.git (push)



    当我为另一个仓库(不在 gitlab 上)运行 git remote -v 时,它返回:

    $ git remote -v

    origin git@myIp:/repository/yyyy/yyyy-order-parent.git (fetch)

    origin git@myIp:/repository/yyyy/yyyy-order-parent.git (push)



    所以我认为这不是远程配置问题......

    当我尝试绕过 ssh 身份验证问题以访问不在 gitlab 上的存储库时,git bash 返回:

    $ git remote -v

    origin git@ip:/repository/yyyy/yyyy-order-parent.git (fetch)

    origin git@ip:/repository/yyyy/yyyy-order-parent.git (push)

    $ git fetch origin

    Enter passphrase for key '/c/Users/user/.ssh/id_rsa':

    git@ip's password:

    GitLab: The project you were looking for could not be found. fatal: Could not read from remote repository.

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



    这是 的内容非gitlab ~/.ssh/config 文件

    Host DEV_XX1

    HostName DEV_XX1

    User gitUser

    IdentityFile /home/hostUser/.ssh/id_rsa

    IdentitiesOnly yes

    Host myIp

    User hostUser

    ProxyCommand ssh deploy@myIp -W %h:%p



    我该如何解决这个问题?我认为它可以存在一个配置解决方案,但是哪一个呢?

    谢谢 !

    最佳答案

    问题是,在您的第二个项目中,远程 url 可能设置为 ssh url,类似于 git@ip/.... (运行命令:“git config -l”查看,注意“远程.origin.url=")

    您需要将 URL 编辑为使用 http 或 git 服务器提供的其他协议(protocol)的 URL。使用以下命令进行编辑:

    git remote set-url origin https://yourusername@servername/username/project.git

    总之,有两种方法可以解决这个问题。首先,使用“remote set-url”命令作为我上面的答案,将您的 url 更改为 http。其次,使用 ssh 认证。您需要生成一个公钥/私钥对,然后将公钥存储到 git 服务器(如果您的 git 服务器支持它)。最后,创建一个具有以下内容的 ~/.ssh/config 文件:
    Host hostname host_ip 
    HostName host_ip
    IdentityFile ~/.ssh/privatekey
    User yourusername

    这样,身份验证将自动处理

    关于git - 如何在有和没有 ssh 身份验证的情况下将 Git 与两个不同的存储库一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38695031/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com