1、准备工作:
下载安装git客户端 http://code.google.com/p/git-osx-installer/downloads/list?can=3(安装了git客户端,命令行中才有git命令)
注册github账号 https://github.com/ -->Pricing and Signup -->Create a free account
2、创建ssh:(创建ssh目的是将mac与github服务器建立互信)
在local打开terminal:
$cd ~/.ssh 检查是否已经存在ssh(.ssh只是一个demo,它指的是mac存放公钥的位置,如果第一次ssh连接,可跳过此步骤)
如果存在,先将已有的ssh备份,或者将新建的ssh生成到另外的目录下
如果不存在,通过默认的参数直接生成ssh
生成过程如下:
$ssh-keygen -t rsa -C [email protected]([email protected]指的是在github注册的email)
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/twer/.ssh/id_rsa): (此处可以走默认,即公钥存放在/Users/twer/.ssh/id_rsa,也可输入绝对路径修改存放位置)
Created directory ‘/Users/twer/.ssh‘.
Enter passphrase (empty for no passphrase): (输入公钥文件的密码)
Enter same passphrase again: (确认公钥文件的密码)
Your identification has been saved in /Users/twer/.ssh/id_rsa.
Your public key has been saved in /Users/twer/.ssh/id_rsa.pub.
The key fingerprint is:
18:16:11:c9:01:6c:48:09:7f:27:c6:43:0d:7f:3f:84 [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
|.o.++=== |
|.ooo.+. . |
| ..* = E . |
| o = + o |
| . S o |
| . |
| |
| |
| |
+-----------------+
此处完成后表示在mac上已经成功生成了与github互信的公钥
在github中添加ssh:
登陆github,选择Account Settings-->SSH Keys 添加ssh
Title:[email protected](title尽量使用自己的邮箱,这个title与登录github账户的邮箱是不同概念,可以不相同)
Key:打开你生成的id_rsa.pub文件,将其中内容拷贝至此(使用cd命令进入id_rsa.pub目录,再使用more id_rsa.pub命令查看公钥的内容)
测试SSH:
$ssh [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(此处表示第一次没连接成功,yes代表再连接一次)
Warning: Permanently added ‘github.com,207.97.227.239‘ (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
(在上面那句话执行完之后可能会让输入用户名和密码,输入在github注册的用户名和密码即可,认证通过后会在本地存储一个凭据,以后用户名和密码就不需要再输入了)
Hi xianfuying! You‘ve successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.(如果出现Hi xxx,证明mac与github成功建立了互信)
设置本地git个人信息:(向github提交数据时会显示下面设置的值,以方便查找谁对这个仓库做了什么事)
$git config --global user.name "your real name"
$git config --global user.email "[email protected]"
至此,git和github的设置就完成了
3、提交代码
$git add *
$git commit -m "your commit‘s reason"(提交代码到本地仓库)
$git remote add alias [email protected]:xxxxx/projectName.git( 定义远程服务器别名为alias)
$git pull -u alias master(将代码从github pull到本地仓库)
$git pull -uf alias master(强制将代码从github pull到本地仓库)
$git push -u alias master(将本地仓库的代码push到github仓库)
$git remote -v(查看远程服务器别名)
$git remote rm alias(删除远程服务器别名)