1、安装Git插件
Myeclipse老版本可能需要安装Git插件(window->preference->Team下出现Git就说明安装成功),高版本中已经安装好了。我所使用的是MyEclipse2016,不需要安装Git插件,所以在这里就不在具体讲述安装Git插件的步骤。
2、生成MyEclipse的一个SSH的认证号
由于Github的代码是开源的,每个人都可以查看代码,但是只有自己能够修改代码。Github通过SSH Key识别出提交的代码是用户本人推送的,才能够针对相应的代码进行操作。即本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以需要本地创建SSH Key并在GitHub服务器上设置SSH Key,从而创建本地和Guthub服务器的关系:window->preferences-->general-->Network Connections-->SSH2
2.1、General中的值为用户主目录下的.ssh文件和该目录下的id_rsa和id_rsa.pub文件(这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人)

2.2、本地创建SSH Key:点击Key Management 中的Generate RSA Key...生成SSH认证号,然后点击Save Pravite Key进行保存(注意:不要将我们之前的公钥和密钥给覆盖了,我们可以选择另外的目录进行保存),然后点击Apply

3、在GitHub上设置SSH Key,从而创建本地和Guthub服务器的关系(将生成的公钥添加到Github中去)
3.1、拷贝公钥文件id_rsa.pub的内容:找到刚刚保存公有秘钥的路径(如下),可以用Notepad打开文件并复制内容

3.2、将从id_rsa.pub公有密钥文件中复制的内容添加到Github的ssh key 设定里:
(1)注册Github账号:打开Github主页https://github.com进行注册,按要求填写用户名、密码和邮箱,并进行邮箱验证
(2)打开github网站的“settings”中的SSH Keys或者直接页面访问https://github.com/settings/ssh
(3)点击右上角 Add SSH key
(4)输入title(任意填写,这里以gittest为例),然后将复制的公有密钥复制粘贴到key中

添加完成后结果

注意:私有密钥和公有密钥是成对的两个文件,私有文件保存在自己的本机,公有密钥保存到另一端的服务器、网站 等。github就是一种网站。只有保存了私有密钥的机器才能访问远程的服务器等。使用该键的好处是不用使用密码,而是以密钥的方式验证用户。要想使本机能访问github。有三个步骤:(1) 创建私有密钥和公有密钥(2)将公有密钥放到github里。(3)测试是否设置成功(4)修改本地git配置文件并发布
4、在Github上创建自己的代码仓库
4.1、Github注册成功后,点击右上角的“+”号,选择New repository开创建代码仓库
(1)Resository name:填写自己定义的仓库名(必填项)
(2)Description:填写描述(选填项)
(3)Public:保持选中(如果选Private可以选择谁能看到谁能提交,public只能选谁能提交,private是要钱的)
(4)Initialize this repository with a README:决定是否生成一个README文件来初始化仓库(可选可不选)
(5)Create repository:点击生成代码仓库

4.2、获取代码仓库的地址:点击Create repository后生成http地址就是该代码仓库的地址,如下图所示

5、创建本地git仓库
5.1、在MyEclipse中创建项目(如gittest)
5.2、右键项目-->team-->share project来共享项目

点击Next,出现如下界面,进行Git代码库配置

点击Finish,我们的工程显示为NO-HEAD,即表示没有指针指向

6、首次提交项目至远程
选中项目右键-->team-->commit
(1)Commit message:填写提交信息
(2)Status:选中需要提交的文件
(3)Commit and Push:提交到本地并推送至Github服务器
(4)Commit:提交到本地

6.1、这里我们选择提交并推送按钮,会出现以下如下界面

(1)首先我们需要填写github中仓库地址(4.2中已获取),同时填写用户名、密码,选中记住

(2)然后点击Next

(3)然后会进行远程通信,如果网络不好可能会有点问题

(4)远程连接成功后,会出现如下界面

(5)点击Finish

(6)此时项目状态如下:即表示没有指针指向的“NO-HEAD”已经消失,项目显示在master分支上

6.2、查看提交信息
(1)查看本地提交信息:右键项目-->team-->Show in History来查看提交信息

(2)查看Github服务器上的提价信息:访问https://github.com/sewenfengqing/development查看提交信息(提交所在分支、提交描述、提交时间等)

为什么在MyEclipse中的gittest已经通过项目右键-->team-->Commit多次,且通过项目右键-->team-->Show in History查看到了提交信息,问什么在Guithub中仅能查到第一次的提交信息,之后却查不到?(注意:通过项目右键-->team-->commit只是提交到本地,而通过项目右键-->team-->push to upstream或者commit中commit and push才会上传到Github服务器)
7、修改代码提交至本地Git

此时仅在本地有提交信息,Github上没有

8、修改代码提交至远程仓库Github
直接项目右键-->team-->commit-->commit and push推送项目至服务器,出现如下问题

为了使用远程推送,您必须指定至少一个URI和至少一个REF映射

解决方案:
(1)创建分支:项目右键-->team-->Switch to-->New Branch

(2)项目显示就在bliy分支上

(3)提交新建的文件
1>右键文件-->team-->Add to Index;

2>此时新建的文件前面会出现“+”号,右键文件-->team-->commit,选择commit;
3>而后我们再进行推送team-->Push branch liy
(后两步可以替换为:右键文件-->team-->commit-->commit and push)

(4)重新提交修改代码并推送至服务器
文件右键-->team-->commit-->commit and push


(5)Giithub上显示提交信息

9、合并分支
(1)首先切换回主分支:team-->Switch to -->master
(2)之后:team-->Merge

(3)选中liy分支,点击Merge

(4)点击ok之后再进行提交推送
10、通过使用egit插件来进行版本控制
10.1、从服务器克隆代码仓库至本地
项目右键-->import-->Git-->Projects from Git

点击Next

点击Next

点击Next,仓库就从远程Clone(克隆)到本地

之后在myeclipse中导入该项目,更改代码后的提交方式请看上面的第六步。
10.2、提交修改的代码到Github服务器
(1)将改动的代码标记
项目右键-->team-->synchronize workspace

点击Yes后

项目右键-->Add to Index ,修改的文件标记为“*”

(2)将工作空间的代码提交到本地仓库
项目右键-->team-->commit
(3)将远程仓库的代码pull(合并)到本地仓库
项目右键-->team-->pull (这一步可能需要解决代码冲突,合并代码 )
(4)将本地仓库代码push(推送)到远程仓库
项目右键-->team-->push to upstream
网友评论