git使用https协议,每次pull, push都要输入密码,相当的烦。
使用git协议,然后使用ssh密钥。这样可以省去每次都输密码。
大概需要三个步骤:
一、本地生成密钥对;
二、设置github上的公钥;
三、修改git的remote url为git协议。
一、生成密钥对。
=============
大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个。生成公钥的过程在所有操作系统上都差不多。首先先确认一下是否已经有一个公钥了。SSH 公钥默认储存在账户的主目录下的
二、添加公钥到你的github帐户
========================
1、查看你生成的公钥:大概如下:
3、然后你复制上面的公钥内容,粘贴进“Key”文本域内。 title域,你随便填一个都行。
4、完了,点击 Add key。
这样,就OK了。然后,验证下这个key是不是正常工作。
三、修改你本地的ssh remote url. 不用https协议,改用git 协议
================================================
可以用git remote -v 查看你当前的remote url
使用git协议,然后使用ssh密钥。这样可以省去每次都输密码。
大概需要三个步骤:
一、本地生成密钥对;
二、设置github上的公钥;
三、修改git的remote url为git协议。
一、生成密钥对。
=============
大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个。生成公钥的过程在所有操作系统上都差不多。首先先确认一下是否已经有一个公钥了。SSH 公钥默认储存在账户的主目录下的
~/.ssh
目录。进去看看:
<pre><code><span class="pln">$ cd </span><span class="pun" style="color:#66660;">~/.</span><span class="pln">ssh </span> <span class="pln">$ ls</span> <span class="pln">authorized_keys2 id_dsa known_hosts config id_dsa</span><span class="pun" style="color:#66660;">.</span><span class="pln">pub</span></code>关键是看有没有用
something
和
something.pub
来命名的一对文件,这个
something
通常就是
id_dsa
或
id_rsa
。有
.pub
后缀的文件就是公钥,另一个文件则是密钥。假如没有这些文件,或者干脆连
.ssh
目录都没有,可以用
ssh-keygen
来创建。该程序在 Linux/Mac 系统上由 SSH 包提供,而在 Windows 上则包含在 MSysGit 包里:
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">$</span><span class="pln"> ssh</span><span class="pun" style="color:#66660;">-</span><span class="pln">keygen </span><span class="pun" style="color:#66660;">-</span><span class="pln">t rsa </span><span class="pun" style="color:#66660;">-</span><span class="pln">C </span><span class="str" style="color:#0880;">"</span><em><span class="str" style="color:#0880;">your_email@youremail.com</span></em><span class="str" style="color:#0880;">"</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="com" style="color:#8800;"># Creates a new ssh key using the provided email</span><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;"># </span><span class="com" style="color:#8800;">Generating public/private rsa key pair.</span><span class="com" style="color:#8800;"> </span> </p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="com" style="color:#8800;"># </span><span class="com" style="color:#8800;">Enter file in which to save the key (/home/</span><em><span class="com" style="color:#8800;">you</span></em><span class="com" style="color:#8800;">/.ssh/id_rsa):</span></p>直接Enter就行。然后,会提示你输入密码,如下(建议输一个,安全一点,当然不输也行):
<pre><span class="typ" style="color:#66066;">Enter</span><span class="pln"> passphrase </span><span class="pun" style="color:#66660;">(</span><span class="pln">empty </span><span class="kwd" style="color:#0088;">for</span><span class="pln"> </span><span class="kwd" style="color:#0088;">no</span><span class="pln"> passphrase</span><span class="pun" style="color:#66660;">):</span><span class="pln"> </span><em><span class="pun" style="color:#66660;">[</span><span class="typ" style="color:#66066;">Type</span><span class="pln"> a passphrase</span><span class="pun" style="color:#66660;">]</span></em><span class="pln"> </span> <span class="com" style="color:#8800;"># </span><span class="com" style="color:#8800;">Enter same passphrase again: </span><em><span class="com" style="color:#8800;">[Type passphrase again]</span></em>完了之后,大概是这样。
<pre><span class="typ" style="color:#66066;">Your</span><span class="pln"> identification has been saved </span><span class="kwd" style="color:#0088;">in</span><span class="pln"> </span><span class="pun" style="color:#66660;">/</span><span class="pln">home</span><span class="pun" style="color:#66660;">/</span><em>you</em><span class="pun" style="color:#66660;">/.</span><span class="pln">ssh</span><span class="pun" style="color:#66660;">/</span><span class="pln">id_rsa</span><span class="pun" style="color:#66660;">.</span><span class="pln"> </span> <span class="com" style="color:#8800;"># </span><span class="com" style="color:#8800;">Your public key has been saved in /home/</span><em><span class="com" style="color:#8800;">you</span></em><span class="com" style="color:#8800;">/.ssh/id_rsa.pub.</span><span class="com" style="color:#8800;"> </span> <span class="com" style="color:#8800;"># </span><span class="com" style="color:#8800;">The key fingerprint is:</span><span class="com" style="color:#8800;"> </span><span class="com" style="color:#8800;"># </span><span class="com" style="color:#8800;">01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db </span><em><span class="com" style="color:#8800;">your_email@youremail.com</span></em>这样。你本地生成密钥对的工作就做好了。
二、添加公钥到你的github帐户
========================
1、查看你生成的公钥:大概如下:
<pre><code><span class="pln">$ cat </span><span class="pun" style="color:#66660;">~</span><span class="str" style="color:#0880;">/.ssh/</span><span class="pln">id_rsa</span><span class="pun" style="color:#66660;">.</span><span class="pln">pub </span> <span class="pln">ssh</span><span class="pun" style="color:#66660;">-</span><span class="pln">rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom</span><span class="pun" style="color:#66660;">/</span><span class="pln">BWDSU </span><span class="typ" style="color:#66066;">GPl</span><span class="pun" style="color:#66660;">+</span><span class="pln">nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlE</span> <span class="typ" style="color:#66066;">LEVf4h9lFX5QVkbPppSwg0cda3</span><span class="pln"> </span><span class="typ" style="color:#66066;">Pbv7kOdJ</span><span class="pun" style="color:#66660;">/</span><span class="typ" style="color:#66066;">MTyBlWXFCR</span><span class="pun" style="color:#66660;">+</span><span class="typ" style="color:#66066;">HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK</span><span class="pun" style="color:#66660;">/</span><span class="lit" style="color:#06666;">7XA</span><span class="pln"> t3FaoJoAsncM1Q9x5</span><span class="pun" style="color:#66660;">+</span><span class="lit" style="color:#06666;">3V</span> <span class="lit" style="color:#06666;">0Ww68</span><span class="pun" style="color:#66660;">/</span><span class="pln">eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw</span><span class="pun" style="color:#66660;">/</span><span class="typ" style="color:#66066;">Pb0rwert</span><span class="pun" style="color:#66660;">/</span><span class="typ" style="color:#66066;">En</span><span class="pln"> mZ</span><span class="pun" style="color:#66660;">+</span><span class="pln">AW4OZPnTPI89ZPmVMLuayrD2cE86Z</span><span class="pun" style="color:#66660;">/</span><span class="pln">il8b</span><span class="pun" style="color:#66660;">+</span><span class="pln">gw3r3</span><span class="pun" style="color:#66660;">+</span><span class="lit" style="color:#06666;">1nKatmIkjn2so1d01QraTlMqVSsbx</span><span class="pln"> </span><span class="typ" style="color:#66066;">NrRFi9wrf</span><span class="pun" style="color:#66660;">+</span><span class="pln">M7Q</span><span class="pun" style="color:#66660;">==</span><span class="pln"> schacon@agadorlaptop</span><span class="pun" style="color:#66660;">.</span><span class="kwd" style="color:#0088;">local</span></code>2、登陆你的github帐户。然后 Account Settings -> 左栏点击 SSH Keys -> 点击 Add SSH key
3、然后你复制上面的公钥内容,粘贴进“Key”文本域内。 title域,你随便填一个都行。
4、完了,点击 Add key。
这样,就OK了。然后,验证下这个key是不是正常工作。
<pre><span class="pln">$ ssh </span><span class="pun" style="color:#66660;">-</span><span class="pln">T git@github</span><span class="pun" style="color:#66660;">.</span><span class="pln">com</span> <span class="com" style="color:#8800;"># Attempts to ssh to github</span>如果,看到:
<pre><span class="typ" style="color:#66066;">Hi</span><span class="pln"> </span><em>username</em><span class="pun" style="color:#66660;">!</span><span class="pln"> </span><span class="typ" style="color:#66066;">You</span><span class="str" style="color:#0880;">'ve successfully authenticated, but GitHub does not</span><span class="str" style="color:#0880;"> </span><span class="str" style="color:#0880;"># </span><span class="str" style="color:#0880;">provide shell access.</span>就表示你的设置已经成功了。
三、修改你本地的ssh remote url. 不用https协议,改用git 协议
================================================
可以用git remote -v 查看你当前的remote url
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">$ git remote </span><span class="pun" style="color:#66660;">-</span><span class="pln">v</span></p><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">origin https</span><span class="pun" style="color:#66660;">:</span><span class="com" style="color:#8800;">//github.com/someaccount/someproject.git (fetch)</span> <span class="pln">origin https</span><span class="pun" style="color:#66660;">:</span><span class="com" style="color:#8800;">//github.com/someaccount/someproject.git (push)</span></p>
可以看到是使用https协议进行访问的。
你可以使用浏览器登陆你的github,在上面可以看到你的ssh协议相应的url。类似如下:
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">git@github</span><span class="pun" style="color:#66660;">.</span><span class="pln">com</span><span class="pun" style="color:#66660;">:</span><span class="pln">someaccount</span><span class="pun" style="color:#66660;">/</span><span class="pln">someproject</span><span class="pun" style="color:#66660;">.</span><span class="pln">git</span> </p>
这时,你可以使用 git remote set-url 来调整你的url。
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln">git remote </span><span class="kwd" style="color:#0088;">set</span><span class="pun" style="color:#66660;">-</span><span class="pln">url origin git@github</span><span class="pun" style="color:#66660;">.</span><span class="pln">com</span><span class="pun" style="color:#66660;">:</span><span class="pln">someaccount</span><span class="pun" style="color:#66660;">/</span><span class="pln">someproject</span><span class="pun" style="color:#66660;">.</span><span class="pln">git</span> </p>
完了之后,你便可以再用 git remote -v 查看一下。
OK。
至此,OK。
你可以用git fetch, git pull , git push, 现在进行远程操作,应该就不需要输入密码那么烦了。