用git通过SSH协议commit代码到GitHub

因为用git通过SSH协议commit代码到GitHub,不用每次都输入账号和密码。所以非常方便,现在我们就来看看需要怎样做。

 

一、如果目前需要每次都输入账号和密码,则可以通过执行命令切换成通过SSH协议登录:

(如果还没有设置SSH请先按照下面第二点提示操作)

git remote rm origin
git remote add origin {git SSH Address}

{git SSH Address} 类似这样的:git@github.com:YourAccount/YourProjectName

可以通过以下方式获取。

 

二、 如何在本地生成SSH key及如何设置GitHub允许通过SSH协议登录。

可参考GitHub官方文档:生成SSH keys (分不同平台Windows/Mac/Linux等) 一下为Mac OSX讲解

  1. Open Terminal. 开命令行

  2. Paste the text below, substituting in your GitHub email address.

    $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

    This creates a new ssh key, using the provided email as a label.

    > Generating public/private rsa key pair.
  3. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.

    输入一个文件名字来存储key,可以什么都不输入直接回车存储到默认的.ssh/下 

    > Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
  4. At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".

    可以为key输入密码,也可以直接回车不加密 

    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]

Adding your SSH key to the ssh-agent 将SSH key加入 ssh-agent:

  1. Start the ssh-agent in the background.

    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
  2. If you're using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/configfile to automatically load keys into the ssh-agent and store passphrases in your keychain.

    这一步可以忽略 

    Host *
      AddKeysToAgent yes
      UseKeychain yes
      IdentityFile ~/.ssh/id_rsa
    
  3. Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.

    $ ssh-add -K ~/.ssh/id_rsa

将生成的public key加入GitHub:

即id_rsa.pub加入GitHub。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值