git有诸多好处,网上都说的很清楚了,在这里我不再赘述。对于我来说,私下里想做一些项目,而又不能很好的保存自己的代码和进行版本控制,这时候,就用到了git。下面,就以我个人为例讲讲git从0开始如何安装使用。
Step1 准备工作
msysgit,下载地址为http://msysgit.github.io/。
Eclipse IDE for Java EE Developers(必须是这个,自带Egit,或者自行安装Egit插件),在Eclipse官网下载,地址http://www.eclipse.org/downloads/。
github账号,github官网注册一个,地址https://github.com/。
廖雪峰的git教程,地址为http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000,讲得很好,我大概花了3个晚上的时间看,如果仅仅是为了安装上,可以先暂时略过,后面再看。
Step2 msysgit安装
一路next就行,注意可能会遇到360的拦截(该死,点击允许本程序所有操作或者安装前就退出360即可)。
Step3 git用户名和邮箱配置
单击开始菜单,打开Git Bush,

输入以下命令
git config --global user.name "Your Name"
git config --global user.email email@example.com
用户名和邮箱替换为自己的,为本台机器所有仓库指定用户名和email地址。
Step4 创建SSH Key
因为本地git仓库和远程的github仓库之间是通过ssh加密的,所以,还需要创建一个SSH Key。
输入以下命令
ssh-keygen -t rsa -C youremail@example.com
同样将邮箱替换为自己的。在路径C盘->用户->用户名,找到.ssh文件夹,打开就可以看到两个文件id_rsa和id_rsa.pub,
打开id_rsa.pub,将其中的内容复制下来(这就相当于一把钥匙,持有钥匙的人可以访问你的内容,下一个小结讲解这把钥匙的用途),下面是我的

Step5 与github上的仓库相关联
注册一个github账号
注册完成之后,点击右上角的settings(就是那个齿轮,设置的图标)

进入设置页面

选SSH Keys,点击add SSH key,就可以添加了,title随便填写,将从id_rsa.pub中复制的内容粘贴到这里。从此,gitgub仓库就能够识别你本地的仓库了。也就是说,你离将本地代码推送到github上,或者将github上的代码克隆到本地,就只差一步了。(可参考廖雪峰前辈的教程先进行在非Eclipse下的操作,更有感觉)
Step6 Egit的使用
首先,登入你的github账号,点击加号,选择New repositrory,创建一个新的仓库,如图所示。

名字叫做test2,其他都默认就好。
点击绿色按钮创建,就创建好了一个github仓库。

将地址复制下来,保存好备用。
打开Eclipse,像平常一样,新建一个web工程,或者其他你常用的工程。我这里工程叫做scott
![{4$7`FVS]283EW(~80FR(JT {4$7`FVS]283EW(~80FR(JT](http://images.cnitblog.com/blog/465427/201409/252120388895169.jpg)
右击项目,选择Team->Share Project,弹出如下窗口。

选择git,next,在弹出的界面上,①先勾选Use or create……,②createrepository,③勾选工程,④finish。

这时候,你就创建好了一个git工程,右击工程,选择Team,你可以像svn一样进行commit,synchronize等操作,但是,remote操作中,push是灰色的,表示还不能将代码推送到github仓库上去。

这时,打开Git Repository视图(具体怎么打开百度去,这都是基本常识了)

右键选择Create Remote,点OK

点击Change,弹出如下界面

URI输入之前在github上拷贝的地址,下面的内容会自动填上。你只需要再填上github上的用户名和密码即可。finish,save。
接下来的几步就简单了,在项目里面随便写点代码,然后先将新增的代码提交到本地,例如,将cat类,先add to index,然后commit。这时候,右键工程,Team->remote中就有了push选项,意味着你可以将本地代码保存在github中了。祝贺你!

点击Push,点击add All Branches Spec,将本地版本库添加,然后Finish,后面一路Next,再次查看github,可以看到代码已经成功推送。

最后,看看我们在github上的代码吧!
