软件测试|Pycharm 版本控制与分支管理
Pycharm 版本控制与分支管理
代码版本控制
VCS (version control system) 版本控制是指对软件开发过程中各种程序代码、配置文件及说明文档等文件变更的管理,是软件配置管理的核心思想之一。版本控制最主要的功能就是记录一个或若干个文件内容变化 ,以便将来查阅特定版本内容,并且记录文件的所有历史变化,随时可恢复到任何一个历史状态。除了记录版本变更外,版本控制的另一个重要功能是并行开发。软件开发往往是多人协同作业,版本控制可以有效地解决版本的同步以及不同开发者之间的开发通信问题,提高协同开发的效率。
PyCharm 集成了大部分流行的版本控制系统,如 Git、Subversion、Mercurial、Perforce,本文以 Git 为例介绍 pycharm 的版本控制功能。
Git 安装与配置
下载与配置 Git
Window 下 Git 安装与配置
要使用 git 来进行版本控制,首先就需要安装 git 工具,git工具下载地址 ,下载之后直接点击安装即可,安装之后需要将 git 配置到环境变量中如下图,我的 git 是安装在了 C:\Program Files\Git 目录下,环境变量需要配置到 bin 目录

打开 cmd 命令行工具,输入 git --version 命令,出现如下信息即表示环境配置成功。

Mac 下 Git 安装与升级
Mac 系统一般自带 git,但是 git 版本可能偏低,可以通过git --version
来进行查看

如果感觉 git 版本太低,可以通过以下两种方式进行升级
一是通过 homebrew 来进行安装(需要有一定基础和动手能力),先安装 homebrew,然后通过 homebrew 安装 Git(使用brew install git
进行安装),具体方法请参考 homebrew 的文档:http://brew.sh/
1.1 安装 homebrew 后,先检查 homebrew 是否需要更新
1.2 通过命令安装最新版本 git,等待结束
1.3 查看 git 安装后路径

图中框着的为 git 的路径
1.4 配置环境变量
1.5 查看 git 版本
二是通过 Xcode 进行安装(Xcode 安装包有点大 7.6G 左右),直接去 AppStore 安装 Xcode,Xcode 集成了 Git,最新版的 Xcode 默认安装“Command Line Tools”,你只需要安装并运行 Xcode 即可完成 Git 的升级了。
重新打开终端,再次输入git --version
查看 git 版本,可以看到 git 版本已经更新到最新版本了。

配置用户名与邮箱

生成密钥


查看密钥

gitee 账号配置
因为目前 github 存在访问不稳定的问题,本教程主要介绍通过 gitee 来进行项目的版本管理。
gitee 配置公钥
我们需要复制上方的公钥信息,将公钥信息配置到 Gitee 中
点击“设置”

点击“SSH 公钥”:

粘贴公钥

到这里就完成了公钥的配置。
注:若未注册 Gitee 账号,需要先行注册,再关联公钥。
Pycharm 配置 git
Window Pycharm 配置 git
在使用 pycharm 进行版本管理之前,我们要先进行 git 信息的配置。
File -> settings -> Version Control

按步骤配置完成后,点击 Test 测试是否配置成功。
Mac Pycharm 配置 git
可以使用 pycharm 的默认 git 配置,如果需要额外配置可以选择菜单“PyCharm”->“Preferences”->“Version Control”->“Git” 进行 git 配置,Mac 显示隐藏文件的快捷键⌘⇧.(Command + Shift + .)

点击 Test 来查看是否配置成功

Pycharm 安装 gitee 插件
pycharm 没有直接集成 gitee,需要先安装 gitee 插件,步骤如下:
在 Setting 中选择 Plugins
在 Marketplace 下搜索框中搜索 git
点击 Install 进行下载安装
安装完后点击重启 pycharm

Pycharm 关联 gitee 账号
File -> Setting -> 搜索 gitee
点击加号,选择 Login To Gitee

输入账号密码进行登陆


本地项目分享到 gitee
选择 VCS -> 选择 Share Project On Gitee

Repository name 上传的项目名(也就是在 Gitee 上创建的库的名称),根据实际情况这里可以改也可以不改,勾选上私有,点击 Share,这时候仓库就创建成功了。

在 Gitee 查看仓库是否创建成功。

创建已经存在,证明创建成功了

add 文件到暂存区
我们有两种方式上传文件到 gitee

add 前文件为红色,添加后文件为绿色

随后,commit 文件,将文件提交到提交到本地仓库,可以输入一下我们的提交信息,方便记录


点击 commit 按钮
commit 之后,我们需要通过 push 将本地仓库内容推送到远程仓库



完成 push 之后,我们可以在 Gitee 上查看是否已经推送成功


这样我们就完成了本地项目通过 git 来进行版本控制的工作。
代码版本管理
现在敏捷开发的模式下,分支开发,主干发布的情况是普遍存在的,我们如何来实现对代码版本的有效管理?pycharm 给我们提供了这样的功能。
单分支存在的问题
代码中至少有一个分支,就是主干分支或称主分支 Master,默认都是在主分支上开发

上图图中绿色节点表示每一个提交 commit

项目往往是并行多人开发的,都在主分支上克隆,然后修改提交,那么主分支就会存在大量的冲突,甚至有一些不完善代码提交,主分支就混乱不堪,不可维护了。
再一个,如果一次提交后,需要发布一个版本,这个版本以后就需要独立维护、开发,而主分支还需要继续发展,这时候就需要引入多分支来对项目进行管理。
多分支创建和使用
进行多分支管理时,我们需要坚持下列原则
分支名必须唯一
不能以 _ 开头
可以使用 / 但是不能以它结尾,被它分割的名称不能以 . 开头
不能包含任何空白字符,Git 的特殊符号

创建新分支
查看当前所处分支
我们可以通过命令或者在界面上查看自己当前所处分支


创建新分支



注:如果勾选了检出分支,会切换到创建的新分支


现在我们要将新的脚本上传到新建的分支上,比如 demo2.py
将提交后的代码推送到远程的 dev 分支

切换到 master 分支,将 dev 分支合并到 master 分支上,并推送至远程 master 分支



将 dev 的代码合并到 master 上,这样就会实现 dev 分支总是开发中的代码,dev 测试、审查后合并 master 中,master 分支都是稳定的代码,可以发布


通过对 Gitee 上远程仓库的检查,我们知道我们完成了对分支的合并。
评论