git 如何使用 SSH 将 IntelliJ 连接到 GitHub

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20632511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:28:38  来源:igfitidea点击:

How do I connect IntelliJ to GitHub using SSH

macosgitgithubsshintellij-idea

提问by OldManLink

I have successfully configured my GitHub client to use SSH, and received the confirmatory email from GitHub that a new SSH key was added. I would like to setup IntelliJ to use SSH as well, so that I don't have to enter my Username and Password every time I interact with GitHub. I also don't want IntelliJ to save my password for me, since I am unsure how secure that would be.

我已成功将 GitHub 客户端配置为使用 SSH,并收到来自 GitHub 的确认电子邮件,说明已添加新的 SSH 密钥。我也想将 IntelliJ 设置为使用 SSH,这样每次与 GitHub 交互时都不必输入用户名和密码。我也不希望 IntelliJ 为我保存我的密码,因为我不确定这有多安全。

The closest I have come so far is that I need to edit my ~/.ssh/config file to tell IntelliJ that there is an SSH key it can use. Unfortunately I have not managed to find an example that works.

到目前为止,我最接近的是我需要编辑我的 ~/.ssh/config 文件来告诉 IntelliJ 有一个它可以使用的 SSH 密钥。不幸的是,我还没有找到一个有效的例子。

Here is my latest attempt at a ~/.ssh/config entry:

这是我对 ~/.ssh/config 条目的最新尝试:

Host IntelliJ
  HostName github.com
  User git
  IdentityFile "/Users/peter/.ssh/github_rsa"
  TCPKeepAlive yes
  IdentitiesOnly yes

I have tried restarting IntelliJ after adding that entry, but to no avail. I'm running IntelliJ Ultimate 12.1.6 on Mac OSX 10.8.5

添加该条目后,我尝试重新启动 IntelliJ,但无济于事。我在 Mac OSX 10.8.5 上运行 IntelliJ Ultimate 12.1.6

回答by kukido

GitHub plugin for IntelliJ lets you to save the password, so you don't have to enter it every time.

IntelliJ 的 GitHub 插件允许您保存密码,因此您不必每次都输入密码。

enter image description here

在此处输入图片说明



With keys

带钥匙

(adapted from Multiple SSH Keys settings for different github account, thanks to CrazyCodercomment):

(改编自不同 github 帐户的多个 SSH 密钥设置,感谢CrazyCoder评论):

  • Create ssh key pair

    $ ssh-keygen -t rsa -C "[email protected]"
    
  • Add key

    $ ~/.ssh/id_rsa_activehacker
    
  • Confirm that the key is added

    $ ssh-add -l
    
  • Modify ~/.ssh/config

    Host github.com-activehacker  
    HostName github.com  
    User git  
    IdentityFile ~/.ssh/id_rsa_activehacker
    
  • 创建 ssh 密钥对

    $ ssh-keygen -t rsa -C "[email protected]"
    
  • 添加密钥

    $ ~/.ssh/id_rsa_activehacker
    
  • 确认密钥已添加

    $ ssh-add -l
    
  • 修改 ~/.ssh/config

    Host github.com-activehacker  
    HostName github.com  
    User git  
    IdentityFile ~/.ssh/id_rsa_activehacker
    



In IntelliJ

在 IntelliJ 中

VCS > Checkout from Version Control > Git

VCS > 从版本控制检出 > Git

Checkout from Version Control

从版本控制签出

Test

测试

Test

测试

As you can see, you will still have to either enter passphrase for the key pair after every IntelliJ relaunch(I believe the passphrase is kept in memory) or let IntelliJ to store it permanently.

如您所见,您仍然必须在每次 IntelliJ 重新启动后输入密钥对的密码(我相信密码保存在内存中)或让 IntelliJ 永久存储它。

回答by Tobias Gerschner

Using IntelliJ 2016.3.3 this seems very straight forward by following the steps below:

按照以下步骤使用 IntelliJ 2016.3.3 这似乎非常简单:

  1. Generate your keypair to use, if none exists already with
  1. 生成要使用的密钥对(如果不存在)

ssh-keygen

ssh-keygen

  1. Add the key to your github profile as per https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

  2. Modify the settings in IntelliJ under | VersionControl > GitHub

  1. 根据https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/将密钥添加到您的 github 配置文件中

  2. 修改 IntelliJ 下的设置 | 版本控制 > GitHub

Tick 'Clone git repositories using ssh'

勾选“使用 ssh 克隆 git 存储库”

IntelliJ Settings Screenshot for Github

Github 的 IntelliJ 设置屏幕截图

Choose 'Native' as 'SSH executable'

选择“本机”作为“SSH 可执行文件”

enter image description here

在此处输入图片说明

回答by Steve Byrne

The answer above by kukido seems to now be out of date; the IntelliJ UI no longer provides the same level of access to the github URL. And, the default "built-in" (to IntelliJ) ssh executable seems to not respect the .ssh/config file settings. What I have found now works is to switch from using the built-in ssh executable to the "native" ssh executable. This is done by visiting Settings... | Version Control | Gitand then choosing Nativein the SSH executablepulldown menu.

kukido 上面的答案现在似乎已经过时了;IntelliJ UI 不再提供对 github URL 的相同级别的访问。而且,默认的“内置”(IntelliJ)ssh 可执行文件似乎不尊重 .ssh/config 文件设置。我发现现在有效的是从使用内置 ssh 可执行文件切换到“本机” ssh 可执行文件。这是通过访问Settings... | Version Control | Git然后NativeSSH executable下拉菜单中选择来完成的。

This applies to IntelliJ version 2016.2 and I'm pretty sure earlier versions such as 2016.1 and the 2015 series.

这适用于 IntelliJ 版本 2016.2,我很确定更早的版本,例如 2016.1 和 2015 系列。

回答by thenaturalist

Steve Byrne is right about the changes to this procedure. As I do not have enough reputation to comment, I would like to add to his answer that I could only make this work with Github when inputing the SSH URL in the prompt for cloning a repo. You can get your repo's SSH URL by clicking on "Clone or Download" and then "Use SSH" on Github, like so:

Steve Byrne 对这个程序的改变是正确的。由于我没有足够的声誉来发表评论,我想在他的回答中补充一点,我只能在克隆 repo 的提示中输入 SSH URL 时才能使用 Github 进行这项工作。您可以通过在 Github 上单击“克隆或下载”然后“使用 SSH”来获取您的存储库的 SSH URL,如下所示:

enter image description here

在此处输入图片说明

回答by abshar

  • Open Git Bash and create a new ssh key using your GitHub email address like this:

    ssh-keygen -t rsa -b 4096 -C "[email protected]"

  • Then it asks you a file path for saving the key. You can simply accept the default by pressing Enter.

  • You then will be asked for a passphrase. Make sure you remember itas you may need it at the time you want to push your project's changes to GitHub.
  • Ensure your ssh agent is running. For that you can enter the following command

    eval $(ssh-agent -s)

  • Now add your SSH private key to the ssh-agent like this:

    ssh-add ~/.ssh/id_rsa

  • Now go to your GitHub profile and select setting->SSH and GPG keys. Select New SSH Keyand after entering a title, copy your key using this command :

    clip < ~/.ssh/id_rsa.pub

  • Finally go to the Git setting of IntelijIDEA and select Built-infor SSH executable.

  • You can try to see if everything works fine by pushing your project to GitHub. You will be asked for the passphrase you entered when creating ssh key.

  • 打开 Git Bash 并使用您的 GitHub 电子邮件地址创建一个新的 ssh 密钥,如下所示:

    ssh-keygen -t rsa -b 4096 -C "[email protected]"

  • 然后它会询问您保存密钥的文件路径。您只需按 Enter 即可接受默认值。

  • 然后,您将被要求输入密码。确保您记住它,因为在您想要将项目的更改推送到 GitHub 时可能需要它。
  • 确保您的 ssh 代理正在运行。为此,您可以输入以下命令

    评估 $(ssh-agent -s)

  • 现在将您的 SSH 私钥添加到 ssh-agent 中,如下所示:

    ssh 添加 ~/.ssh/id_rsa

  • 现在转到您的 GitHub 个人资料并选择setting->SSH and GPG keys。选择New SSH Key并在输入标题后,使用以下命令复制您的密钥:

    剪辑< ~/.ssh/id_rsa.pub

  • 最后转到 IntelijIDEA 的 Git 设置并选择Built-infor SSH executable

  • 您可以通过将您的项目推送到 GitHub 来尝试查看是否一切正常。创建 ssh 密钥时,系统会要求您输入密码。