目录
[1、配置 Git 忽略文件](#1、配置 Git 忽略文件)
1、配置 Git 忽略文件
作用:与项目的实际功能无关,不参与服务器上部署/实际代码的执行。把它们忽略掉能够屏蔽 IDE 工具之 间的差异。
配置步骤:
1、安装.ignore插件
- File --- Setting --- Plugins --- 输入.ignore --- install【安装.ignore插件】
2、创建.gitignore文件
- File --- New --- .ignore file --- .gitignore file (Git) --- 创建.gitignore文件

3、编辑.gitignore文件
- 打开.gitignore文件,此时系统已经默认忽略了某些文件;也可以手动添加或删除要忽略的文件。
.gitignore 文件,每行的忽略规则的语法如下:
- 空格不匹配任意文件,可作为分隔符,可用反斜杠转义
- #开头:标识注释,可以使用反斜杠进行转义
- ! 开头:标识否定,该文件将会再次被包含,如果排除了该文件的父级目录,则使用 ! 也不会再次被包含。可以使用反斜杠进行转义
- / 结束:只匹配文件夹以及在该文件夹路径下的内容,但是不匹配该文件
- / 开头:匹配文件
- 如果一个模式不包含斜杠,则它匹配相对于当前 .gitignore 文件路径的内容,如果该模式不在 .gitignore 文件中,则相对于项目根目录
- ** 匹配多级目录,可在开始,中间,结束
- ? 通用匹配单个字符
- [] 通用匹配单个字符列表
常用匹配示例:
- bin/ :忽略当前路径下的bin文件夹,该文件夹下的所有内容都会被忽略,不忽略 bin 文件
- /bin :忽略根目录下的bin文件
- /*.c :忽略 cat.c,不忽略 build/cat.c
- debug/*.obj : 忽略 debug/io.obj,不忽略 debug/common/io.obj 和 tools/debug/io.obj
- **/foo : 忽略/foo, a/foo, a/b/foo等
- a/**/b : 忽略a/b, a/x/b, a/x/y/b等
- !/bin/run.sh : 不忽略 bin 目录下的 run.sh 文件
- *.log : 忽略所有 .log 文件
- config.php : 忽略当前路径的 config.php 文件
4、.gitignore文件更改后不生效怎么办?
原因:新增的忽略文件只对工作区生效,对在暂存区的文件、本地仓库、远程仓库的文件是无效的,所以最好在新建项目之初就新建忽略文件。
解决办法:
- 1、先删除暂存区的文件
- 2、再使用命令 git add . 将本地文件添加到暂存区中
- 3、然后再提交到本地仓库
- 4、最后再推送到远程仓库
git rm -r --cached .
git add .
git commit -m "update .gitignore"
5、pycharm中使用git后的各文件颜色说明:
- 被.ignore忽略的文件,颜色是:黄色
- 在本地工作区的文件,颜色是:红色
- 添加暂存区的文件,颜色是:绿色
- 提交到本地仓库的文件,颜色是:变成正常色 白色
- 推送到远程仓库的文件,颜色是:白色
2、定位Git
1、File->Settings->Git

3、使用pycharm本地提交
3.1、初始化本地库
1、VCS->Create Git Repository

2、选择需要初始化本地库的文件

3、初始化成功后所有文件颜色变成:红色 所有文件待添加到暂存区

3.2、添加到暂存区
1、右键项目->Git->Add 将项目添加到暂存区

2、可以看到项目颜色变成绿色,说明已经添加到暂存区了

3.3、提交到本地库
1、右键项目->Git->Commit Directory 将项目提交到本地仓库

2、选择需要提交的项目,并备注提交信息。

3.4、切换版本
1、在 pycharm 的左下角,点击 Git,然后点击 Log 查看版本


2、选项需要切换的历史版本->右键->Checkout Revision (对应的版本号的前8位)

3、查看切换版本是否成功
- 切换之前的内容和指针指向情况

- 切换后的内容和指针指向情况

4、分支操作
4.1、创建分支
1、项目右键->Git->New Branch 或者 右下角版本号->New Branch


2、填写分支名称,创建 hot-fix 分支。


4.2、切换分支
1、项目右键->Git->Bramches->选择需要切换的分支->点击">"->点击Checkout


或者 点击右下角hot-fix分支->点击需要切换的分支->点击Checkout 进行分支切换

2、查看分支是否切换成功

4.3、合并分支
1、在 pycharm 窗口的右下角,将 hot-fix 分支合并到当前 master 分支。
- 点击右下角master分支->点击hot-fix分支->点击Merge Selected into Current 进行分支合并
- 如果代码没有冲突,分支直接合并成功,分支合并成功以后,代码自动提交,无需手动 提交本地库。

5、解决冲突
注意:在实际工作中,在本地合并分支时,通常需要先切换到目标分支(如master
),然后拉取该分支最新的代码,再进行合并操作。
1、如图所示,如果 master 分支和 hot-fix 分支都修改了代码,在合并分支的时候就会发生 冲突。
注意:修改文件一定要先提交到本地仓库,然后切换到master分支,最后把hot-fix分支合并到master分支,再把合并后的代码提交到本地仓库


2、切换搭配master 分支,然后合并 hot-fix 分支,就会发生代码冲突。


3、点击Merge按钮,手动合并代码

4、手动合并完代码以后,点击右下角的 Apply 按钮。

5、手动合并完成后,需要手动提交到本地仓库
- 过程同上:先添加到暂存区,然后提交到本地仓库

