配置git push時無需輸入用戶名和密碼

使用github,在git push時,需要輸入用戶名和密碼,這會很不方便,本文介紹使用ssh來實現無需輸入用戶名和密碼。

場景:我有一個repository:https://github.com/strongyoung/Notes,我要實現對這個repository的git push不用輸入用戶名和密碼。

生成key

找到自己的公共key,切換到家目錄,進入.ssh目錄下,看下有沒有公共key,如果有,跳過這一步,如果沒有,使用以下命令進行生成。

[yang@master ~]$ cd 
[yang@master ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yang/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/yang/.ssh/id_rsa.
Your public key has been saved in /home/yang/.ssh/id_rsa.pub.
The key fingerprint is:
38:fb:83:ff:2c:60:08:a2:0b:c8:e8:0b:b6:c0:c5:71 yang@master
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|   . E           |
|. o o  .         |
|=. + .o S        |
|*.. . oo         |
|=+   ..o         |
|=..   ..o.       |
| o.    .o+o      |
+-----------------+
[yang@master ~]$ ls .ssh/
id_rsa      id_rsa.pub 

對於生成key有疑問,可以參考以下:
https://help.github.com/articles/generating-an-ssh-key/

部署key

登錄github,找到相應的repository,點擊“setting”,然後點左邊的“Deploy keys”,再點右邊的”add deploy key”,如下圖:
這裏寫圖片描述

在Title輸入標題,在key的輸入框中輸入剛纔生成的公有key,即id_rsa.pub的內容。再在“allow write access”前面打上勾。如下:

這裏寫圖片描述

部署key也可以參考以下:
https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

測試ssh 連接

使用以下命令進行測試

[yang@master Notes]$ ssh -T [email protected]
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.
Hi strongyoung/Notes! You've successfully authenticated, but GitHub does not provide shell access.

已成功連接,如有其他問題,可以參考以下:
https://help.github.com/articles/testing-your-ssh-connection/

配置remote url

使用以下命令進行配置remote url:

[yang@master Notes]$ git remote set-url origin git@github.com:strongyoung/Notes.git

這裏的[email protected]:strongyoung/Notes.git換成你自己的repository,注意,這個是ssh 地址,不是https地址:
這裏寫圖片描述
在“clone download”處點擊“use SSH”,可以找到ssh的clone地址。


測試

[yang@master Notes]$ git push origin master
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 503 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To [email protected]:strongyoung/Notes.git
   31da6f8..d7bb118  master -> master

恭喜,在push的時候,已無需輸入用戶名和密碼。

參考文獻:
[1] http://stackoverflow.com/questions/6565357/git-push-requires-username-and-password

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章