如何向git帳號上提交代碼

官方說明:https://help.github.com/articles/generating-ssh-keys/html

1,爲Github帳戶設置SSH keyjava

文章地址:http://zuyunfei.com/2013/04/10/setup-github-ssh-key/linux

什麼是SSH key

一直使用SSH鏈接服務器,可是對它的原理卻不太瞭解。此次設置Octopress的時候,須要使用SSH 方式鏈接Github, 正好對SSH的工做方式作了下了解。(好像Github推薦使用HTTPS的方式訪問repo, 之前Github受到過SSH密匙攻擊,以後升級了SSH key的安全措施,https方式視乎更方便安全,不過Octopress的設置文檔中,我並無找到怎麼使用HTTPS鏈接Github)

簡單來講,SSH提供了兩種級別的安全驗證:git

  1. 第一種級別是基於密碼的安全驗證,知道帳號和密碼,就能夠登錄到遠程主機。Team的開發工做中,就是使用這種方式登錄編譯服務器,或者開發機器。由於是在內網中,這種級別的安全驗證已經足夠了。
  2. 第二種級別是基於Public-key cryptography (公開密匙加密)機制的安全驗證,原理以下圖所示:

其優勢在於無需共享的通用密鑰,解密的私鑰不發往任何用戶。即便公鑰在網上被截獲,若是沒有與其匹配的私鑰,也沒法解密,所截獲的公鑰是沒有任何用處的。github

產生SSH key

根據Github提供的help文檔,具體過程以下shell

1
2
$ cd ~/.ssh
# Checks to see if there is a directory named ".ssh" in your user directory 


使用ssh-keygen產生新的key安全

1
2
3
4
$ ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key using the provided email Generating public/private rsa key pair. Enter file in which to save the key (/home/you/.ssh/id_rsa): 


使用默認的文件名直接enter, 按提示輸入密碼(若是不提供密碼,SSH將無密碼鏈接,若是private key泄露可能會有安全問題)服務器

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


密匙產生成功ssh

1
2
3
4
Your identification has been saved in /home/you/.ssh/id_rsa.
Your public key has been saved in /home/you/.ssh/id_rsa.pub. The key fingerprint is: 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com 

上傳public key到Github帳戶

  1. 登陸github
  2. 點擊右上方的Accounting settings圖標
  3. 選擇 SSH key
  4. 點擊 Add SSH key

在出現的界面中填寫SSH key的名稱,填一個你本身喜歡的名稱便可,而後將上面拷貝的~/.ssh/id_rsa.pub文件內容粘帖到key一欄,在點擊「add key」按鈕就能夠了。
添加過程github會提示你輸入一次你的github密碼ide

設置SSH使用HTTPS的403端口

在局域網中SSH的22端口可能會被防火牆屏蔽,能夠設置SSH使用HTTPS的403端口。

測試HTTPS端口是否可用

1
2
3
$ ssh -T -p 443 git@ssh.github.com
Hi username! You've successfully authenticated, but GitHub does not provide shell access. 


編輯SSH配置文件 ~/.ssh/config 以下:

1
2
3
Host github.com
  Hostname ssh.github.com
  Port 443


測試是否配置成功

1
2
3
$ ssh -T git@github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

多個Github帳號的SSH key切換

若是在一臺機器上要登錄多個Github帳戶,須要一些配置,雖然如今並無用到,可是先記下來以備不時之需,過程參看這裏

 

2,【GitHub】解決每次push代碼到github都須要輸入用戶名和密碼的方法

在github上,創建一個項目test,去主頁查看能夠看到

 

若是使用HTTPS:

 

Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/guochy2012/test.git git push -u origin master

Push an existing repository from the command line

git remote add origin https://github.com/guochy2012/test.git git push -u origin master

 

 

若是採用SSH:

 

Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:guochy2012/test.git git push -u origin master

Push an existing repository from the command line

git remote add origin git@github.com:guochy2012/test.git git push -u origin master

 

 

使用HTTPS須要每次輸入密碼,SSH則不用,但SSH須要配置密鑰 。

關於怎麼產生密鑰能夠參見《Generating SSH Keys》一文

 

3,github地址 從https改爲ssh

打開命令行工具,運行 git remote set-url origin 例如:

而後再次 commit,若是出現相似:

字樣,那麼說明你的 SSH key 沒有設置或已經失效(譬如升級到 Mountain Lion 系統後),請從新參照上文的官方文檔進行設置便可。

4,執行pull時報錯

wangkongming@AY140527171808170503Z:~/github/collect$ git pull
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/wangkongming/.ssh/id_rsa' are too open.
It is recommended that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /home/wangkongming/.ssh/id_rsa
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

答案:http://stackoverflow.com/questions/1556119/ssh-private-key-permissions-using-git-gui-or-ssh-keygen-are-too-open

是由於給 id_rsa的權限過高了,改爲700就能夠了。也有人說600

You changed the permissions on the whole directory, which I agree with Splash is a bad idea. If you can remember what the original permissions for the directory are, I would try to set them back to that and then do the following

cd ~/.ssh
chmod 700 id_rsa

inside the .ssh folder. That will set the id_rsa file to rwx (read, write, execute) for the owner (you) only, and zero access for everyone else.

If you can't remember what the original settings are, add a new user and create a set of SSH keys for that user, thus creating a new .ssh folder which will have default permissions. You can use that new .ssh folder as the reference for permissions to reset your .ssh folder and files to.

If that doesn't work, I would try doing an uninstall of msysgit, deleting ALL .ssh folders on the computer (just for safe measure), then reinstalling msysgit with your desired settings and try starting over completely (though I think you told me you tried this already).

Edited: Also just found this link via Google -- Fixing "WARNING: UNPROTECTED PRIVATE KEY FILE!" on Linux While it's targeted at linux, it might help since we're talking liunx permissions and such.

 

======================================

如下是本身在使用git時,總結的:

1,查看當前項目遠程分支的路徑

git remote -v

相關文章
相關標籤/搜索