【Git&Github总结】4. Eclipse中使用Git
一、Ecplise中的Git插件
打开window--Preferences
【提示】大多数的Ecplise的版本都是自带git插件的。
二、Ecplise的工程初始化为本地库
https://blog.csdn.net/weixin_42247720/article/details/81270621
这就相当于git init的命令。
三、在Ecplise中设置本地库的签名
Window ---> perferences ---> Team ---> Git ---> Configuration --> Respository Setting
选择 需要设计的本地库 GitInit2!
点击Add Entry,自定义签名信息
四、Ecplise中Git图标的介绍
Window ---> perferences ---> Team ---> Git ---> Label Decorations
五、Ecplise中特定文件的介绍
在创建完上述的项目之后,在工程上---> 右键 ---> Team ---> commit 会看到以下视图
实际上,项目中我只创建四个文件,在提交的时候会将编译文件、eclipse特定文件都列举出来,对我们很干扰,能不能忽略不要显示这些文件是个问题!
如何忽略这字特定的文件呢?
(1)新建文件java.gitignore
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.classpath
.project
.settings
target
(2)在~/.gitconfig 文件中引入上述文件
找到gitconfig全局配置文件,默认在:C:\Users\用户\ 文件夹下面
添加如下
[core]
// 路径指向忽略配置文件位置,这也是为什么忽略配置文件命名随意原因
excludesfile = C:/Users/Administrator/Java.gitignore
【注意】这里路径中一定要使用“/”,不能使用“\”
(3)重启Eclipse
六、Ecplise中本地库的基本操作
1. git add
方式一:在工程上---> 右键 ---> Team ---> Add to Index
方式二:在工程上---> 右键 ---> Team ---> Commit
2. git commit -m "message" 文件
在工程上---> 右键 ---> Team ---> Commit 如果文件未到暂存区,先把文件拖到暂存区,然后输入提交备注信息,最后点击提交按钮即可
七、推送、克隆
https://blog.csdn.net/xiaozhegaa/article/details/84844000
八、解决冲突
有两个项目GitInit2(本地创建,推送到远程库)、GitInit3(从远程库克隆下来)
这里GitInit2和GitInit3表示两个人同时操作一个项目
1. 冲突演示
(1)在com.dgut.edu.cn.controller.GitInitController中的main方法如下:
public static void main(String[] arg) {
System.out.println("test test");
}
(2)GitInit2项目的人将其改成
public class GitInitController {
public static void main(String[] arg) {
System.out.println("GitInit2 test...........");
}
}
(3)GitInit3项目的人将其改成
public class GitInitController {
public static void main(String[] arg) {
System.out.println("GitInit2 test...........");
}
}
(4)此时:GitInit2项目的人先提交了项目代码到远程库
先提交到本地库,然后使用remote--->push推送到远程库
(5)GitInit3项目的人后提交代码到远程库,push推送到远程库,出现版本冲突
2. 解决冲突
(1)项目 ---> 右键 ---> team ---> pull
【提示】拉取下来的工程会和GitInit3合并,所以会出现冲突
(2)手动解决冲突,上面有说过。
(3)解决完之后,add-commit-push
九、分支
项目一TestGit、项目二TestGit2。项目二是项目一的克隆。项目一、项目二一开始都是在主分支Master
1. 右键项目一 ---- 创建分支
2. 分支创建完毕之后,该项目会自动切换到hot-fix分支
你个人可以在这个分支下完成你的功能,之后add-commit-push
3. 项目二在Master分支, 拉取
右键项目二--Team--Pull,会自动检测到刚才新创建的分支hot-fix
4. 项目二切换到hot-fix分支
右键项目二 --- Team -- Switch to --- Other
5.检查新分支的功能
6.确认功能开发没问题值后,切换到主分支执行合并
右键 --- Team --- Switch to --- master,项目二切换到Master分支
右键 --- Team --- Merge
合并成功后,把 master 推送到远程。
这就是分支协同开发!