分享 初学者连载系列之二:Git 使用指南 (上)

kevinhua · 2012年04月06日 · 最后由 sqsy 回复于 2014年01月16日 · 11877 次阅读

系列文章原载于自己的博客,TOPI.CO (http://topi.co) ,某天不小心就 push 错啦,懒得从头再来,上传到 Ruby-China 来,一是方便自己回顾,另外也方便跟我一样的初学者

下载并安装最新版 Git

GitHub 基于开源的版本控制系统 (Version Control System, CVS)——Git*。所以与 Git 息息相关,第一步我们需要安装最新版的 Git。

具体步骤,参见后续文章:

  • 在 CentOS 5.6 (64 位) 上部署 Rails
  • 在 Ubuntu 10.04.03 LTS (64 位) 上部署 Rails

###设置 SSH Keys

GitHub 通过 SSH Keys 在本地和 GitHub 建立安全连接。设置 SSH Keys 相当容易,需要下面几个步骤:

  • 检查 SSH Keys

如果已经有键值对,可以跳至第 4 步。 首先,我们要检查一下本地是否已经存在 SSH Keys

$cd ~/.ssh



如果提示“No such file or directory“,则跳至第 3 步。否则继续第 2 步。

  • 备份和删除已经存在的 SSH Keys

因为主目录下已经存在 SSH 目录,所以需要先进行备份:

$ls
config  id_rsa  id_rsa.pub  known_hosts
$mkdir key_backup
$cp id_rsa* key_backup
$rm id_rsa*



  • 生成新的 SSH Key

输入以下命令生成新的 SSH Key。我们希望生成默认设置,所以在询问输入存储 key 的文件 (file in which to save the key) 时,直接按回车。

$ssh-keygen -t rsa -C "[email protected]"
Genereting public/private rsa key pair.
Enter file in which to save the key
(/Users/your_user_directory/.ssh/id_rsa):<press enter>



直接按回车后,会提示输入密码 (passphrases):

Enter passphrase (empty for no passphrase):<enter a passphrase>
Enter same passphrase again:<enter passphrase again>



输入密码后,屏幕会显示与下面类似的提示信息:

Your identification has been saved in
/Users/your_user_directory/.ssh/id_rsa
Your public key has been saved in
/Users/your_user_directory/.ssh/id_rsa.pub
The key figerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]
The key's randomart image is:
+--- RSA 2048]----+
|     .+   +      |
|       = o  O .  |
|        =  *  *  |
|       o  =  +   |
|      o  S  .    |
|     o  o  =     |
|      o  .  E    |
|                 |
+-----------------+



  • 将 SSH Key 添加到 GitHub

在 GitHub 页面上,点击"Account Settings" > 点击"SSH Public Keys" > 点击"Add another public key"

{% img /images/account_settings.jpg 252 61 [GitHub Accoun Settings] %}

用 vim 或 geditor 打开 id_rsa.pub 文件,这就是你的 public SSH Key。将整段 public SSH Key 复制并粘贴在"Key"文本框内:

{% img /images/public_ssh_key_field.jpg 372 268 [Public SSH Key Field] %}

  • 测试

为确保设置成功,现在可以测试设置成果啦。记住,"[email protected]"是默认的,无需修改。

$ ssh -T [email protected]



如果设置成功的话,应该显示:

The authenticity of host 'github.com (207.97.227.239)' 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",系统会显示:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.



设置个人信息

现在,你已经成功安装 Git,并设置好 SSH Keys,是设置个人信息的时候啦:

  • 设置用户名和电子邮件
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "[email protected]"



  • 设置 GitHub 令牌 (token)

在 GitHub 页面,点击"Account Settings" > 点击 "Account Admin",可以查看你的 API Token。

{% img /images/api_token.jpg 372 296 [API Token] %}

然后在命令行输入:

$ git config --global github.user username
$ git config --global github.token 0123456789your0123456789token



Note: 如果你更改 GitHub 密码,会创建新的 Token,需要重新设置。

最后一个环节:开始庆祝吧!

这篇我看了,蛮简单的

之前如果建立 ssh 连接, 只要將公鑰複製到 ~/.ssh/authorized_keys 就可以利用金鑰登入 而不需要建立密碼。

現在的 ssh 使用同樣的方法會出現錯誤訊息

Agent admitted failure to sign using the key

解決方式 使用 ssh-add 指令將私鑰 加進來(根据个人的密匙命名不同更改 id_rsa)

ssh-add ~/.ssh/id_rsa

需要 登录 后方可回复, 如果你还没有账号请 注册新账号