如何指定在Git上执行shell命令时使用的私有SSH密钥?

Chr*_*fer 963 git ssh bash shell

也许是一个相当不寻常的情况,但我想指定一个私有SSH密钥,以便在从本地计算机执行shell(git)命令时使用.

基本上是这样的:

git clone git@github.com:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"
Run Code Online (Sandbox Code Playgroud)

甚至更好(在Ruby中):

with_key("/home/christoffer/ssh_keys/theuser") do
  sh("git clone git@github.com:TheUser/TheProject.git")
end
Run Code Online (Sandbox Code Playgroud)

我见过使用Net :: SSH连接到远程服务器的示例,它使用指定的私钥,但这是一个本地命令.可能吗?

Hey*_*his 1097

这些解决方案都不适合我.

相反,我详细阐述了@Martinv.Löwis提到config为SSH 设置文件.

SSH将查找用户的~/.ssh/config文件.我的设置如下:

Host gitserv
    Hostname remote.server.com
    IdentityFile ~/.ssh/id_rsa.github
    IdentitiesOnly yes # see NOTES below
Run Code Online (Sandbox Code Playgroud)

我添加了一个远程git存储库:

git remote add origin git@gitserv:myrepo.git
Run Code Online (Sandbox Code Playgroud)

然后git命令对我来说正常工作.

git push -v origin master
Run Code Online (Sandbox Code Playgroud)

笔记

  • IdentitiesOnly yes需要防止SSH默认行为发送所述身份文件匹配每个协议的默认的文件名的.如果您有一个名为的文件~/.ssh/id_rsa将在~/.ssh/id_rsa.github没有此选项之前尝试.

参考

  • 我发现当你使用.ssh/config指定多个密钥时,你需要在"主机"行中使用主机朋友名称作为"git remote add"命令的一部分.如果line是"Host stg",那么你需要使用git remote add <someName> user @ stg:/path_to_git_repo.git".如果你使用精确的服务器名称如user@myserver.com:/path_to_git_repo.git,配置文件没有被git选中.因此,它没有正确选择私钥文件.我通过将相同的内容推送到github和heroku来尝试这个,只有当你在"git remote add"中给出友好名称时才能工作 (14认同)
  • 您可以使用“Host remote.server.com”并继续使用原始 URL (3认同)
  • 我不确定github的主机.我找到了这个链接:https://gist.github.com/jexchan/2351996. (2认同)
  • 经过两次更改,这对我有用。如果配置文件是新的,请不要忘记执行chmod 600〜/ .ssh / config(请参阅[here](https://superuser.com/questions/232373/how-to-tell-git-which -私钥使用))。如果您使用的是GitHub,则将“ Host gitserv”替换为“ Host github.com”,省略“ Hostname remote.server.com”,并添加git remote add origin git@github.com:user_name / repo_name.git `。 (2认同)

Mar*_*wis 715

这样的东西应该工作(由orip建议):

ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git'
Run Code Online (Sandbox Code Playgroud)

如果您更喜欢子壳,您可以尝试以下(虽然它更脆弱):

ssh-agent $(ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git)
Run Code Online (Sandbox Code Playgroud)

Git将调用SSH,它将通过环境变量找到它的代理; 反过来,这将加载密钥.

另外,设置HOME也可以做的伎俩,只要你愿意建立一个仅包含一个目录.ssh的目录HOME; 这可能包含identity.pub或配置文件设置IdentityFile.

  • 固定命令行(对于Windows或Linux)将类似于:`ssh-agent bash -c'ssh-add sshkey; git clone url'` (116认同)
  • 不,当git完成时,ssh-agent终止,忘记密钥. (27认同)
  • 这个命令不能在windows git bash上运行.它说意外令牌'ssh-add'附近的语法错误 (6认同)
  • 但这会永久性地将密钥添加为可接受的SSH密钥,对吧?我想避免这种情况,以便用户2不会弄乱用户的项目.这是一个Web应用程序,因此使用不同的操作系统用户是不切实际的,这本来是最好的选择. (5认同)
  • orips的路线为我工作; 马丁没有. (4认同)
  • `$(..)` 语法可能没有你所期望的那样:它首先运行内部的命令并将它们的输出发送到 `ssh-agent`。如果另一个`ssh-agent`碰巧已经在运行,这可能无论如何都会起作用,但是新密钥将留在代理中(除非您添加`ssh-add -d`将其取出)并且答案变为`ssh-add /somewhere/yourkey &amp;&amp; git clone git@github.com:user/project.git ; ssh-add -d /somewhere/yourkey`(我们假设 `ssh-agent` 已经在运行,并注意使用了 `&amp;&amp;` 而不是 `;`)。但这没有实际意义,因为`bash -c` 版本是正确的。 (3认同)
  • ssh-agent $(..)语法对我不起作用,我不确定这应该如何工作:(ba)sh应该先在$(..)内部执行命令,然后再执行使用输出作为参数运行ssh-agent。 (2认同)
  • 如果密钥有密码,此解决方案将如何工作? (2认同)
  • 你也可以用`ssh-add -t 60; git clone git@github.com:user/project.git`。那只会在 60 秒后使密钥过期。 (2认同)

phi*_*reo 396

其他人的建议~/.ssh/config更复杂.它可以很简单:

Host github.com
  IdentityFile ~/.ssh/github_rsa
Run Code Online (Sandbox Code Playgroud)

  • 这仅适用于github只有1个用户帐户的情况 (33认同)
  • 你也需要`IdentitiesOnly`选项. (22认同)
  • @EnzeChi你可以通过操作遥控器来拥有多个github帐户:`git remote add ssh://personal/org/proj.git && git remote add ssh:// corporate/org/proj.git`.然后你配置看起来像`Host personal HostName github.com ... Host corporate HostName github.com` (3认同)
  • 不完全适合粒度。我的公司在 github 上有一个组织,而我在 github 上有一个个人帐户,因此仅使用主机并不能很好地工作。 (3认同)
  • 我的工作没有“IdentitiesOnly”选项。有人可以解释为什么需要这样做吗? (2认同)

Rob*_*ill 379

从Git 2.3.0开始,我们也有简单的命令(不需要配置文件):

GIT_SSH_COMMAND='ssh -i private_key_file' git clone user@host:repo.git
Run Code Online (Sandbox Code Playgroud)

您可能需要重新启动计算机上的ssh服务.

  • 简单易用的解决方案.如果你需要做很多事,我建议创建一个别名. (7认同)
  • 如果这个答案还包括“-o IdentitiesOnly=yes”,以确保使用“-i”指定的密钥(而不是来自 SSH 代理的密钥),那就太好了。 (7认同)
  • 不要忘记使用chmod 400 &lt;私有密钥文件路径&gt;。否则git命令可能会失败,并且没有特殊错误消息... (3认同)
  • 我得到`不能运行ssh -i /home/vagrant/.ssh/git:没有这样的文件或目录`虽然它存在``444 Nov 16 18:12/home/vagrant/.ssh/git`来自`ls -l /家用/流浪者/的.ssh/git` (2认同)
  • @ted:chmod 400 /home/vagrant/.ssh/git (2认同)
  • 这应该是公认的解决方案 (2认同)

Joe*_*ock 136

my_git_ssh_wrapper的内容:

#!/bin/bash

ssh -i /path/to/ssh/secret/key $1 $2
Run Code Online (Sandbox Code Playgroud)

然后你可以通过这样做来使用密钥:

GIT_SSH=my_git_ssh_wrapper git clone git@github.com:TheUser/TheProject.git
Run Code Online (Sandbox Code Playgroud)

  • 如果您在同一个域中拥有多个帐户,那么这是一个非常好的解决方案,其他解决方案处理得不好 (4认同)
  • 此解决方案还涵盖了在没有主目录的情况下从帐户使用git时的情况. (2认同)

Dan*_*scu 97

总结答案和评论,设置git以使用不同的密钥文件然后忘记它的最佳方法,它也支持同一主机的不同用户(例如个人GitHub帐户和工作帐户),适用于Windows同样,是编辑~/.ssh/config(或c:\Users\<your user>\.ssh\config)并指定多个身份:

Host github.com
HostName github.com
IdentityFile /path/to/your/personal/github/private/key
User dandv

Host github-work
HostName github.com
IdentityFile /path/to/your/work/github/private/key
User workuser
Run Code Online (Sandbox Code Playgroud)

然后,要将项目克隆为个人用户,只需运行常规git clone命令即可.

要将repo克隆为workuser,运行git clone git@github-work:company/project.git.

  • 我认为这比@ thamster更好,只是因为它解释了主机别名. (11认同)
  • 我低估了你,因为你所说的一切已经在上面的答案中得到了解释,在我的眼里,甚至更清楚.例如,为什么要分别将用户定义为e dandv和workuser? (3认同)
  • 我喜欢这个答案。但是,对我来说,这仅在我将“IdentitiesOnly yes”添加到我的 ssh 配置文件时才有效。 (3认同)
  • 你回答了一个4岁的问题没有新的信息,你声称你的答案是"最好的方式".此外,你贬低和骚扰其他用户删除他们的答案...只是为了让你的一个推高. (2认同)

Von*_*onC 83

使用Git 2.10+(Q3 2016年公布的九月2D,2016),你必须设置一个可能配置GIT_SSH_COMMAND(而不仅仅是一个环境变量中描述罗伯特·杰克威尔回答)

提交3c8ede3通过(2016年6月26日)阮泰玉维战(pclouds).
(由Junio C gitsterHamano合并- -提交dc21164,2016年7月19日)

添加了一个新的配置变量,core.sshCommand以指定每个存储库使用GIT_SSH_COMMAND的值.

core.sshCommand:
Run Code Online (Sandbox Code Playgroud)

如果这个变量被设置,git fetch并且git push将使用指定的命令,而不是ssh当他们需要连接到远程系统.
该命令与GIT_SSH_COMMAND环境变量的格式相同,并在设置环境变量时被覆盖.

这意味着git clone可以是:

cd /path/to/my/repo
git config core.sshCommand 'ssh -i private_key_file' 
# later on
git clone host:repo.git
Run Code Online (Sandbox Code Playgroud)

  • @Spanky您可以执行内联命令`git -c core.sshCommand =“ssh -i private_key_file”clone host:repo.git`,然后执行配置集`git config core.sshCommand'ssh -i private_key_file'` (3认同)
  • 这绝对是最好的答案! (3认同)
  • 作品.人们应该认为这是最好的答案.一旦发布,可以提供信息,以便将.git/config文件与预先复制到/ tmp的版本区分开来.已经创建了一个新条目:sshCommand = ...为了它的价值,我使用了'git config core.sshCommand"ssh -i $ HOME/.ssh/privatekeyfile". (2认同)
  • 这很好用,谢谢。必须解决 Windows 上的另一个问题,如果您使用本机 Windows ssh 和 Windows ssh-agent 服务,您将需要指定 Windows ssh 的路径,否则它将使用 git 附带的 ssh,您必须输入每次都输入密码。```git config core.sshCommand 'C:/Windows/System32/OpenSSH/ssh.exe -i C:/Users/firstname.lastname/.ssh/mygitkey_ed25519 -o IdentitiesOnly=yes' ``` (2认同)

Dav*_*vid 67

如上所述:https://superuser.com/a/912281/607049

您可以按每个仓库配置它:

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
git pull
git push
Run Code Online (Sandbox Code Playgroud)

  • http://linuxcommand.org/man_pages/ssh1.html,指定没有配置文件,所以当git运行ssh时,不会传递配置文件(实际上它是一种沙箱模式,只是忽略用户配置默认选项)超级用户中的线程有关于-F的更多信息 (3认同)

Ric*_*pie 67

最快、最简单的方法是:

使用 ssh 克隆您的存储库:

git -c core.sshCommand="ssh -i ~/.ssh/<your_key>" clone git@github.com:<user>/<repo>.git

然后cd进入您克隆的存储库并:

git config core.sshCommand 'ssh -i ~/.ssh/<your_key>'

要测试它是否有效:

git --git-dir=/path/to/repo/.git pull

所以你可能想知道:为什么我创建的 ssh 密钥在我将 .pub 植入 github 后不起作用,而 private 位于默认目录中?

文档为我们提供了一个命令来澄清该问题: ssh -vT git@github.com

输出显示 git 查找的 ssh 密钥名称列表。因此,您可能希望使用这些名称之一创建密钥,或者使用上述过程来包含您需要的名称。


Fli*_*imm 36

此命令克隆存储库并将 SSH 密钥配置为永久使用:

git clone -c "core.sshCommand=ssh -i ~/.ssh/<key>" git@github.com:<user>/<repo>.git
Run Code Online (Sandbox Code Playgroud)

现在,如果您运行git fetchgit pullgit push,它将使用在 中配置的 SSH 密钥core.sshCommand(保存在 中.git/config)。


tha*_*ter 33

更好的方法是将主机或ip添加到.ssh/config文件中,如下所示:

Host (a space separated list of made up aliases you want to use for the host)
    User git
    Hostname (ip or hostname of git server)
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_(the key you want for this repo)
Run Code Online (Sandbox Code Playgroud)

  • 这不一定是真的.我为Github使用了多个密钥 - 一个用于工作,一个用于我的个人帐户.您不必为"主机"设置域名.你可以放任何你想要的别名.例如,我使用gh-home和gh-work作为我的主机名,当我克隆时,我使用,例如,`git clone git @ gh-work:repo/project.git`在我的〜/ .ssh/config中我有两个部分都使用github.com作为HostName.他们只有不同的IdentityFile和Host (10认同)

Jam*_*mie 30

我选择了GIT_SSH环境变量.这是我的包装器,类似于上面的Joe Block,但处理任意数量的参数.

文件〜/ gitwrap.sh

#!/bin/bash
ssh -i ~/.ssh/gitkey_rsa "$@"
Run Code Online (Sandbox Code Playgroud)

然后,在我的.bashrc中,添加以下内容:

export GIT_SSH=~/gitwrap.sh
Run Code Online (Sandbox Code Playgroud)


car*_*org 24

GIT_SSH_COMMAND="ssh -i /path/to/git-private-access-key" git clone $git_repo
Run Code Online (Sandbox Code Playgroud)


cie*_*ego 15

从 Git 版本 2.10.0 开始,您可以为每个 repo 或全局配置它

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -o 'IdentitiesOnly yes'"
Run Code Online (Sandbox Code Playgroud)

这将为当前存储库指定将使用什么 ssh 密钥。我假设如果你想指定这个全局只需要设置--global选项。

  • 您需要使用 git init 初始化 git (2认同)

小智 14

问题是当您在同一主机上不同的远程存储库(例如github.com),并且您想使用不同的ssh密钥(即不同的GitHub帐户)与它们进行交互时。

为了做到这一点:

1)首先,您应该在〜/ .ssh / config文件中声明不同的密钥。

# Key for usual repositories on github.com
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# Key for a particular repository on github.com
Host XXX
HostName github.com
User git
IdentityFile ~/.ssh/id_other_rsa
Run Code Online (Sandbox Code Playgroud)

这样,您将第二个密钥与github.com的新友好名称“ XXX”相关联。

2)然后,您必须更改特定存储库的远程来源,以便它使用您刚刚定义的友好名称。

在命令提示符下转到本地存储库文件夹,并显示当前的远程来源:

>git remote -v
origin  git@github.com:myuser/myrepo.git (fetch)
origin  git@github.com:myuser/myrepo.git (push)
Run Code Online (Sandbox Code Playgroud)

然后使用以下命令更改原点:

>git remote set-url origin git@XXX:myuser/myrepo.git
>git remote -v
origin  git@XXX:myuser/myrepo.git (fetch)
origin  git@XXX:myuser/myrepo.git (push)
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用右键自动推,取...。


cgn*_*utt 11

如果这里没有其他解决方案对您有用,并且您已经创建了多个ssh-key,但仍然无法完成诸如

git pull
Run Code Online (Sandbox Code Playgroud)

然后假设您有两个ssh密钥文件,例如

id_rsa
id_rsa_other_key
Run Code Online (Sandbox Code Playgroud)

然后在git repo内部,尝试:

# Run these commands INSIDE your git directory
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_other_key
Run Code Online (Sandbox Code Playgroud)

并通过以下方式确保您的github默认用户名和用户ID正确:

# Run these commands INSIDE your git directory
git config user.name "Mona Lisa"
git config user.email "mona.lisa@email.com"
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参见https://gist.github.com/jexchan/2351996

  • 请注意,如果您收到“无法打开与身份验证代理的连接。”,请尝试“$ eval \`ssh-agent -s \”,然后重试。 (2认同)

cho*_*tik 9

当你需要一个正常的请求(连接到github上git pull origin master),设置主机为*~/.ssh/config工作对我来说,任何其他主机(比如,"github上"或"国标")不能正常工作.

Host *
    User git
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_xxx
Run Code Online (Sandbox Code Playgroud)


Pau*_*die 9

其中许多解决方案看起来很诱人.但是,我发现以下链接中的通用git-wrapping-script方法是最有用的:

如何使用git命令指定ssh密钥文件

关键是没有如下git命令:

git -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
Run Code Online (Sandbox Code Playgroud)

Alvin的解决方案是使用一个明确定义的bash-wrapper脚本来填补这个空白:

git.sh -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
Run Code Online (Sandbox Code Playgroud)

在哪里git.sh:

#!/bin/bash

# The MIT License (MIT)
# Copyright (c) 2013 Alvin Abad
# https://alvinabad.wordpress.com/2013/03/23/how-to-specify-an-ssh-key-file-with-the-git-command

if [ $# -eq 0 ]; then
    echo "Git wrapper script that can specify an ssh-key file
Usage:
    git.sh -i ssh-key-file git-command
    "
    exit 1
fi

# remove temporary file on exit
trap 'rm -f /tmp/.git_ssh.$$' 0

if [ "$1" = "-i" ]; then
    SSH_KEY=$2; shift; shift
    echo "ssh -i $SSH_KEY \$@" > /tmp/.git_ssh.$$
    chmod +x /tmp/.git_ssh.$$
    export GIT_SSH=/tmp/.git_ssh.$$
fi

# in case the git command is repeated
[ "$1" = "git" ] && shift

# Run the git command
git "$@"
Run Code Online (Sandbox Code Playgroud)

我可以验证这解决了我用用户/密钥识别具有用于远程到位桶回购一个问题git remote update,git pullgit clone; 所有这些现在在一个cron工作脚本中工作正常,否则在导航有限shell时遇到问题.我也能从R中调用这个脚本,仍然可以解决完全相同的cron执行问题(例如system("bash git.sh -i ~/.ssh/thatuserkey.pem pull")).

不是R和Ruby一样,但如果R可以做到...... O :-)

  • 除了语法之外,如何比"GIT_SSH_COMMAND ="ssh -i~/.ssh/thatuserkey.pem"git clone clone thatuser@myserver.com:/ git/repo.git`更好[罗伯特杰克威尔的回答] (http://stackoverflow.com/a/29754018/27358)? (3认同)

Rtm*_*tmY 9

很多很好的答案,但其中一些假设具有先前的管理知识。

我认为重要的是要明确强调的是,如果你通过克隆该网页的网址开始你的项目 -https://github.com/<user-name>/<project-name>.git
那么你需要确保url [remote "origin"].git/config 被改为SSH URL(见下面的代码块)。

除此之外,请确保添加sshCommmand如下所述:

user@workstation:~/workspace/project-name/.git$ cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    sshCommand = ssh -i ~/location-of/.ssh/private_key -F /dev/null <--Check that this command exist
[remote "origin"]
    url = git@github.com:<user-name>/<project-name>.git  <-- Make sure its the SSH URL and not the WEB URL
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
Run Code Online (Sandbox Code Playgroud)

在此处阅读更多相关信息。

  • 非常感谢,花了很多时间试图弄清楚为什么 git 不使用 ssh 密钥。我不明白为什么 github 在克隆按钮中默认提供 https url。 (2认同)

小智 9

为什么不将身份密钥的位置添加到特定存储库的 git 配置文件中,如下所示:

cd .git

vi config

[core]
    sshCommand = ssh -i <IDENTITY_KEY_LOCATION> -o IdentitiesOnly=yes
Run Code Online (Sandbox Code Playgroud)

这就是你所需要的。


cri*_*bal 8

如果您的路径中有要使用给定识别文件进行签名的目录,您可以通过 .ssh/config 文件指定使用特定识别文件,方法是设置ControlPath例如:

host github.com
  ControlPath ~/Projects/work/**
  HostName github.com
  IdentityFile ~/.ssh/id_work
  User git
Run Code Online (Sandbox Code Playgroud)

然后ssh在给定的工作路径下执行 git 命令时将使用指定的身份文件。


Raf*_*mes 8

我只需要添加密钥然后再次运行 git clone。

ssh-add ~/.ssh/id_rsa_mynewkey
git clone git@bitbucket.org:mycompany/myrepo.git
Run Code Online (Sandbox Code Playgroud)


Fat*_*tie 7

2021. 如果您使用的是 Mac。

假设您在 aws 上有一个 ubuntu 服务器,您通常像这样连接到它:

% ssh -i blah/yourkeypair.pem ubuntu@test.fattie.com
Run Code Online (Sandbox Code Playgroud)

在终端只是

% export GIT_SSH_COMMAND="ssh -i /Users/fattie/Desktop/blah/yourkeypair.pem"
Run Code Online (Sandbox Code Playgroud)

在你这样做之后。然后就可以自由...

% git clone ubuntu@test.fattie.com:/home/ubuntu/teste.git  
Run Code Online (Sandbox Code Playgroud)

这会将您服务器上的 repo 克隆到您的本地文件夹“teste”,

然后,您可以在 teste/ 中自由执行通常的命令,例如 ...

% git push origin master
Run Code Online (Sandbox Code Playgroud)

等等。

——

另请注意:https : //stackoverflow.com/a/67287133/294884


至于在服务器上,看来你基本上

] git clone --bare the-actual-folder teste.git
Run Code Online (Sandbox Code Playgroud)

然后在 teste.git

] git init --bare --shared
Run Code Online (Sandbox Code Playgroud)


Pey*_*avi 5

在带有 Git Bash 的 Windows 中,您可以使用以下命令添加存储库

ssh-agent bash -c 'ssh-add "key-address"; git remote add origin "rep-address"'
Run Code Online (Sandbox Code Playgroud)

例如:

ssh-agent bash -c 'ssh-add /d/test/PrivateKey.ppk; git remote add origin git@git.test.com:test/test.git'
Run Code Online (Sandbox Code Playgroud)

哪个私钥在D盘,电脑的文件夹test。另外,如果你想克隆一个存储库,你可以git remote add origingit clone.

将此输入到 Git Bash 后,它会要求您输入密码!

请注意,openssh 私钥和 putty 私钥是不同的!

如果您使用 puttygen 创建了您的密钥,您必须将您的私钥转换为 openssh!


Gil*_*oot 5

这里给出的大部分答案都没有解释最基本的用法的细节。

linux在云中设置服务器(在本例中为服务器)后,您可以从终端使用 ssh 连接到它。

dyson-ubuntu-vm.pem在您的计算机上,将云服务提供商(例如 Azure、AWS 等)提供给您的私钥添加到本地计算机上的 .ssh 配置中,如下所示:

将文件复制.pem到该/home/ssenyonjo/.ssh文件夹​​,然后打开/home/ssenyonjo/.ssh/config文件并添加以下条目:

Host 20.85.213.44
  HostName 20.85.213.44
  User Dyson
  IdentityFile /home/ssenyonjo/.ssh/dyson-ubuntu-vm.pem
  IdentitiesOnly yes

Run Code Online (Sandbox Code Playgroud)

现在从您的终端访问云 Linux 服务器,如下所示:

ssh Dyson@20.85.213.44
Run Code Online (Sandbox Code Playgroud)

当它起作用时,在云服务器上创建一个 git 项目,如下所示:

Dyson@dyson-ubuntu-vm:~/projects$ git init --bare s2
Run Code Online (Sandbox Code Playgroud)

现在回到本地计算机并克隆该空存储库,如下所示:

ssenyonjo@ssenyonjo-pc:~/Projects/mastering-git$ git clone ssh://Dyson@20.85.213.44/home/Dyson/projects/s2
Run Code Online (Sandbox Code Playgroud)

如果您看到类似于以下内容的错误:fatal: Could not read from remote repository,则意味着您访问了错误的文件夹。确保您已经概述了从根到创建的存储库的正确路径。

如果您不想设置配置文件但想访问需要密钥的 ssh 服务器,您可以使用以下命令:

GIT_SSH_COMMAND='ssh -i ~/Projects/aws/keys/aws_ubuntu.pem'  git clone ssh://ubuntu@15.207.99.158/home/ubuntu/projects/mastering-git/rand 
Run Code Online (Sandbox Code Playgroud)

您可以导出该命令以继续将其用于其他任务,例如git pushgit pull

export GIT_SSH_COMMAND='ssh -i ~/Projects/aws/keys/aws_ubuntu.pem'
Run Code Online (Sandbox Code Playgroud)

请参阅:/sf/answers/2082781291/


归档时间:

查看次数:

953159 次

最近记录:

6 年,2 月 前