如何使用 Git 和两个不同的存储库,有和没有 ssh 身份验证?

How to use Git with two differents repositories with and without ssh authentification?

我有一个问题:

我必须参与项目:

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

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

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

当我 运行 git remote -v 用于 gitlab 项目时,git bash returns :

$ git remote -v

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

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

当我 运行 git remote -v 用于另一个 repo(不在 gitlab 上)时 returns :

$ 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 returns :

$ 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" 看看,注意"remote.origin.url=")

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

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

总而言之,有两种方法可以解决这个问题。首先,使用 "remote set-url" 命令作为我上面的回答,将您的 url 更改为 http。其次,使用ssh认证。您需要生成一个 public/private 密钥对,然后将 public 密钥存储到 git 服务器(如果您的 git 服务器支持它)。最后,创建一个包含以下内容的 ~/.ssh/config 文件:

Host hostname host_ip 
HostName host_ip 
IdentityFile ~/.ssh/privatekey
User yourusername

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