前言
在软件开发的过程中,我们会经常使用eclipse 这款IDE作为开发工具,在管理项目,协同项目开发过程中,也会经常用到SVN和Git的一些项目管理工具。这篇文章,主要介绍如何在eclipse中使用git插件,来管理项目的版本,项目的协同开发,包括如何利用git插件将项目代码提交到本地仓库和远程仓库中。以方便自己回头查阅。
用到软件版本
- eclipse Version: Oxygen.3a Release (4.7.3a)
步骤
- 1.首先下载git插件
- 2.关键:配置远程仓库的信息,这里是配置了github的远程仓库,包括用户名和邮箱,SSH秘钥等
- 3.如何将项目推送到本地仓库
- 4.如何将项目推送到github仓库
- 5.一些常用的操作记录和遇到的问题解决
开始
-
1.首先下载git插件
打开eclipse--->help--->Eclipse Marketplace 搜索框中输入git,点击Go
会在插件列表中看到如下的插件,点击Installed,eclipse会自动下载安装
image.png
完成后,点击Window--->Preferences输入框中输入git会看到git插件的具体信息,即安装成功

-
2.配置远程仓库的信息,这里是配置了github的远程仓库,包括用户名和邮箱,SSH秘钥等
继续上步的操作,点击Configuration,在User Setting 的选项的右边点击Add Entry
填写你注册github的用户名和邮箱
image.png
配置ssh秘钥,eclipse生成SSH2 key:Windows -- >Perferences--->General--Network Commection--->SSH2-->Key Management--General RSA Key--->Save Private Key,如图

然后把生成的SSH秘钥粘贴到github上:如图,打开你的github的主页,在右上角点击你的头像,在下拉菜单中点击Settings

然后,看到下图操作,选择SSH and GPG keys 选择New SSH key

然后把eclipse生成的SSH秘钥复制粘贴到下图的框框中,可以填上该秘钥的title,然后Add SSH key

看到下图可以看出添加的SSH,github也会给你发来邮件提示

记得要测试该秘钥是否能够正常使用:方法:打开git的命令窗口,执行命令:$ ssh -T git@github.com,如图下,便是秘钥可以正常使用,这里 git config --list 是查看git的配置,包括用户名和邮箱等

然后在github上新建一个远程仓库,如图下,注意的是,仓库名要与eclipse中新建的项目名称相同


选择你创建的项目,然后右键选择--->Team--->Share Project--->Git--->next


如下图,勾选Use or create repository in parent folder of project,勾选项目,然后点击Create Repository 点击finish 此时已经把eclipse的项目地址作为本地仓库

此时的项目有些文件文件如下图会自带一个?,可以看到有个.gitignore文件,这个文件主要是过滤掉一些不必要的文件,如target文件下的文件


然后把项目推送到本地仓库,选中项目右键,选择Team---->Add to Index

可以看到,项目的文件自带的?变成了*(若是不想添加到版本库中则“Remove from Index”该命令前提是为未commit,若是已经commit那么该命令不起作用)

最后是推送到github远程仓库上:选择项目右键,Team--->commit:(首次提交后,会自动生成master分支)

如下图,填写提交的信息,可以查看到Staged Changes 中变化的文件

这里点击Commit是只提交到本地仓库,而Commit and Push 是推送到本地仓库的同时还push到github远程仓库中去,这里我们直接选择Commit and Push 会弹出下图,填写github新建的仓库地址等信息

把仓库地址复制粘贴到eclipse中的URI中去,填写User和Password即是你github的用户和密码




出现如下的错误,不能提交到远程仓库

类似于在git的命令窗口输入git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支后,要先git pull origin master 一下,再执行git push -u origin master的效果一样,不然,提交的时后总会出现这样的错误,提交不成功
即
git 提交的步骤:
-
git init //初始化仓库
-
git add .(文件name) //添加文件到本地仓库
-
git commit -m "first commit" //添加文件描述信息
-
git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支
-
git push -u origin master //把本地仓库的文件推送到远程仓库
提交之后就会出现以下错误

要想解决以上错误,只需要在4,5之间使用git pull origin master即可
正确步骤:
-
git init //初始化仓库
-
git add .(文件name) //添加文件到本地仓库
-
git commit -m "first commit" //添加文件描述信息
-
git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支
-
git pull origin master // 把本地仓库的变化连接到远程仓库主分支
-
git push -u origin master //把本地仓库的文件推送到远程仓库
在eclipse中的解决方法:选择项目右键,Team--->remote---->pull--->close


再进行 team--->remote--->next---->push---->next 然后按照下图的,

点击Source ref和Destination ref,重新选择refs/heads/master ,然后再点击右边的Add Spec

看下图,会自动把分支添加到下面的地方,最后finish即可顺利完成把项目push到github远程仓库中去


再到github远程仓库中,就可以查看到刚刚push过去的项目啦

一些常用的操作记录
-
1.当出现如下问题时,可以执行命令:git remote rm origin 进行删除了,再添加
image.png
-
2.当出现如下问题时,可以先按照上面的操作,在git的命令窗口输入git remote add origin + 远程仓库地址 ,创建主分支后,要先git pull origin master 一下,再执行git push -u origin master
image.png
-
3.如何用命令查看,修改remote url:
如果你在连接github仓库的时候,用的https协议的地址,还需要切换为git协议
可以用git remote -v 查看你当前的remote url,可以看到是使用https协议进行访问的。
image.png
这时,你可以使用 git remote set-url 来调整你的url。完了之后,你便可以再用 git remote -v 查看一下。如:git remote set-url origin git@github.com:chenrongjian/test1.git
image.png
-
4.查看本地SSH Key是否正常:$ ssh -T git@github.com
出现如下信息表示设置成功:
Hi username! You've successfully authenticated, but GitHub does not # provide shell access.如下:
image.png
备注:
1.使用eclipse插件提交项目至远程必须先本地提交,最后提交至远程仓库
2.查看谁提交了代码,项目右击“team”--“show history”
3.查看提交记录:Team--Show in History (命令行为:git log)
4.撤销恢复修改:Replace With--HEAD Revision (命令行为 git checkout head .)
5.更新项目远程仓库:Team-- Pull (命令行:git pull https://github.com/someone/xxx.git)
6.查看哪些文件被修改了:Team--Synchronize Workspace
7.提交项目时注意忽略不必要的文件或文件夹如maven项目下的target文件夹忽略
8.git提供了多重协议用来连接git服务器,其中最常见的就是https和git,git就是可以免用户名和密码的,不同的协议,项目连接地址是有区别的,如下
https形式:https://github.com/chenrongjian/test1.git
git形式:git@github.com:chenrongjian/test1.git
参考文章:https://www.cnblogs.com/qq3111901846/p/7889553.html 感谢作者
网友评论