簡體   English   中英

使用 SSH 的 git clone 只能在 Git Bash 中工作,而不是在 Windows CMD 上

[英]git clone with SSH only working in Git Bash not on Windows CMD

所以,我跟着這個關於如何設置SSH與Windows CMD和所有工作正常github上,直到我去克隆一個回購教程

git clone git@github.com:{myusername}/{myrepo}.git

我在哪里

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

即使當我運行ssh -T git@github.com時,我也會收到預期的消息,告訴我我已通過身份驗證。

撓了一陣腦后,我決定在 git bash 上嘗試一下。

我注意到的第一件事是跑步

 ssh-add -l

在 git bash 中,我得到The agent has no identities. 但是當我在 Windows CMD 上運行相同的命令時,我得到了所有的 SSH 密鑰?

因此,在 git bash 中添加我的 ssh 密鑰后,我能夠克隆我的存儲庫。

那么,為什么我只能在 git bash 上執行此操作,而不能在 cmd 或 powershell 上執行此操作? 這與他們似乎使用不同的 ssh 代理有關嗎? 如果是這種情況,我如何將它們同步在一起?

此外,當我運行以下命令時

ssh -Tv git@github.com

用我得到的 cmd

debug1: identity file C:\\Users\\{myuserdirectory}/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory

但是用 git bash 我得到

debug1: identity file /c/Users/{myuserdirectory}/.ssh/id_rsa type 0

另一個區別是,在 Windows cmd 中,我沒有得到任何debug1: Will attempt key: ....實例debug1: Will attempt key: ....

當我退出 git bash 並打開另一個 git bash 終端,再次運行ssh-add -l時,它返回The agent has no identities. 即使在我之前添加它之后,它也只是在每個會話中持續存在,這也是不可取的。

對此的任何幫助將不勝感激!

看起來您的 ssh-agent 沒有運行或無法識別您的 ssh 密鑰

嘗試這個:

# add the default ~/.ssh keys to the ssh-agent
ssh-add

# restart the ssh-agent
eval $(ssh-agent)

# On windows:
start-ssh-agent

ssh-add

ssh-add 將 RSA 或 DSA 身份添加到身份驗證代理 ssh-agent。
不帶參數運行時,它會添加文件~/.ssh/id_rsa~/.ssh/id_dsa~/.ssh/identity

可以在命令行上給出替代文件名

可能你是對的,他們使用了不同的 ssh 代理。 我遇到了完全相同的問題,這個答案對我有很大幫助: https : //stackoverflow.com/a/40720527/6486458

默認情況下,git 指的是它自己在C:\\Program Files\\Git\\usr\\bin的 ssh。 我添加了GIT_SSH環境變量並將其設置為C:\\Windows\\System32\\OpenSSH\\ssh.exe 這可以防止 ssh 版本之間的不一致。 之后 git 開始按預期從 Git Bash 和 Windows cmd 工作。

從 git 文檔

如果指定了GIT_SSH ,它是一個在 Git 嘗試連接到 SSH 主機時調用的程序,而不是 ssh。 它的調用方式類似於$GIT_SSH [username@]host [-p <port>] <command>

另請參閱此答案: https : //stackoverflow.com/a/8713121/6486458

如果您安裝 Git bash,Windows 上會出現一個奇怪的錯誤。 打開命令提示符,然后執行

ls ~/.ssh

如果您發現此文件夾已創建,則將公鑰和私鑰從您的用戶文件夾復制到此路徑:

cp C:\Users\username\.ssh\id_* ~/.ssh/

出於某種原因,Windows 命令提示符會在您第一次執行 git clone 時創建此路徑,之后它只會請求 git@gitlab / git@github 密碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM