使用 Git 进行版本控制
程序完成既定功能后,发布一个版本。
添加或修改部分功能后,生成一个新版本。
对这些版本进行管理,需要使用Git。
管理包括:
1、了解一共产生了多少个版本
2、每个版本之间发生的具体内容是什么
3、将主版本恢复到之前的状态
4、在主版本之上,建立分支版本
------------------------------------------
1、Git安装及配置
从官网下载适合本机操作系统的Git并安装。
在Windows系统中完成安装后,运行Git CMD。
配置用户
$ git config --global user.name "_username_"
$ git config --global user.email "_username@example.com_"
2、创建项目并使用Git管理
1)创建项目文件夹,并在其中添加项目文件。
2)创建一个名为.gitignore的特殊文件,忽略项目中__pycache__文件夹内的修改。因为这些修改时python自动生成的,不需要管理版本。在文件.gitignore中写入如下内容:
__pycache__/
3)初始化Git仓库。
在Git CMD中跳转到项目文件夹。执行初始化命令:git init。
运行结果如下,表明在项目文件夹git_practice下创建文件夹.git。这个.git文件夹就是Git仓库。
git_practice$ git init
Initialized empty Git repository in git_practice/.git/
4)把文件加入仓库。
命令git add .将项目中未被跟踪的所有文件都加入仓库中,并使用命令git status查看仓库状态。显示位于master分支上,没有新的提交,添加了两个被跟踪文件。
git_practice$ git add .
git_practice$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: hello_git.py
3、向仓库中提交修改。
提交的命令是git commit。-m表示要添加记录,此处记录的内容是:Started project。
提交后使用命令git status查看状态,显示位于master分支,working tree clean,这是每次提交项目的可行状态时都希望看到的消息。
git_practice$ git commit -m "Started project."
[master (root-commit) ee76419] Started project.
2 files changed,4 insertions(+)
create mode 100644 .gitignore
create mode 100644 hello_git.py
git_practice$ git status
On branch master
nothing to commit,working tree clean
git_practice$
提交后,可以使用git log命令查看提交日志。每次提交时,Git都会生成唯一的引用ID,长40字符。
git_practice$ git log
commit a9d74d87f1aa3b8f5b2688cb586eac1a908cfc7f (HEAD ->master)
Author:Eric Matthes <eric@example.com>
Date: Mon Jan 21 21:24:28 2019 -0900
Started project.
git_practice$
对hello_git.py文件进行修改,再运行git status命令时,发现有没有提交的修改。
git_practice$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello_git.py
no changes added to commit (use "git add" and/or "git commit -a")
git_practice$
执行命令git commit -am "Extended greeting."再次提交。-a让Git将仓库中所有修改了的文件都加入当前提交中。
git_practice$ git commit -am "Extended greeting."
[master 51f0fe5] Extended greeting.
1 file changed,1 insertion(+),1 deletion(-)
git_practice$ git status
On branch master
nothing to commit,working tree clean
git_practice$ git log --pretty=oneline
51f0fe5884e045b91c12c5449fabf4ad0eef8e5d (HEAD ->master) Extended greeting.
ee76419954379819f3f2cacafd15103ea900ecb2 Started project.
git_practice$
如果两次提交之间,对现有文件的修改使用git commit -a提交,添加新文件需要git add .
4、检出上次提交的版本,使用命令git checkout .撤销提交后的修改
git_practice$ git checkout .
git_practice$ git status
On branch master
nothing to commit,working tree clean
git_practice$
5、根据git log检出任意一次提交的版本
git_practice$ git log --pretty=oneline
51f0fe5884e045b91c12c5449fabf4ad0eef8e5d (HEAD ->master) Extended greeting.
ee76419954379819f3f2cacafd15103ea900ecb2 Started project.
git_practice$ git checkout ee7641
Note:checking out 'ee7641'.
You are in 'detached HEAD'state.You can look around,make experimental
changes and commit them,and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create,you may
do so (now or later) by using -b with the checkout command again.Example:
git checkout -b <new-branch-name>
HEAD is now at ee7641...Started project.
git_practice$
在执行命令git log之后,看到历次提交的版本ID
使用命令git checkout ID前六位,就可以检出到该ID对应的版本。
注意,这时已经离开master分支,进入了detached HEAD状态。
如果想在这个版本的基础上建立一个分支,应该使用命令git checkout -b <new-branch-name>
如果需要回到master分支,执行命令git checkout master
如果要回到以前的版本,并且仍在master分支,执行命令git reset --hard ID前六位
6、删除仓库
如果提交记录混乱,可以在检出需要的版本之后,删除git仓库,并重建仓库。
删除仓库命令:rm -rf .git
git_practice$ rm -rf .git
git_practice$ git status
fatal:Not a git repository (or any of the parent directories):.git
git_practice$ git init
Initialized empty Git repository in git_practice/.git/
推荐阅读
-
使用 git 命令和 vscode 从零开始从远程代码库中拉取、提交、合并和推送分支,并在项目中进行练习
-
YOLOv8 的改进 | 检测头 | 使用 DynamicHead 添加辅助检测头进行定向检测(四检测头版本)
-
使用 Python 进行渗透测试 - 版本:Python3.X
-
一种结构设计模式,允许在对象中动态添加新行为。它通过创建一个封装器来实现这一目的,即把对象放入一个装饰器类中,然后把这个装饰器类放入另一个装饰器类中,以此类推,形成一个封装器链。这样,我们就可以在不改变原始对象的情况下动态添加新行为或修改原始行为。 在 Java 中,实现装饰器设计模式的步骤如下: 定义一个接口或抽象类作为被装饰对象的基类。 公共接口 Component { void operation; } } 在本例中,我们定义了一个名为 Component 的接口,该接口包含一个名为 operation 的抽象方法,该方法定义了被装饰对象的基本行为。 定义一个实现基类方法的具体装饰对象。 公共类 ConcreteComponent 实现 Component { public class ConcreteComponent implements Component { @Override public void operation { System.out.println("ConcreteComponent is doing something...") ; } } 定义一个抽象装饰器类,该类继承于基类,并将装饰对象作为一个属性。 公共抽象类装饰器实现组件 { protected Component 组件 public Decorator(Component component) { this.component = component; } } @Override public void operation { component.operation; } } } 在这个示例中,我们定义了一个名为 Decorator 的抽象类,它继承了 Component 接口,并将被装饰对象作为一个属性。在操作方法中,我们调用了被装饰对象上的同名方法。 定义一个具体的装饰器类,继承自抽象装饰器类并实现增强逻辑。 公共类 ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component 组件) { super(component); } } public void operation { super.operation System.out.println("ConcreteDecoratorA 正在添加新行为......") ; } } 在本例中,我们定义了一个名为 ConcreteDecoratorA 的具体装饰器类,它继承自装饰器抽象类,并实现了操作方法的增强逻辑。在操作方法中,我们首先调用被装饰对象上的同名方法,然后添加新行为。 使用装饰器增强被装饰对象。 公共类 Main { public static void main(String args) { Component 组件 = new ConcreteComponent; component = new ConcreteDecoratorA(component); 组件操作 } } 在这个示例中,我们首先创建了一个被装饰对象 ConcreteComponent,然后通过 ConcreteDecoratorA 类创建了一个装饰器,并将被装饰对象作为参数传递。最后,调用装饰器的操作方法,实现对被装饰对象的增强。 使用场景 在 Java 中,装饰器模式被广泛使用,尤其是在 I/O 中。Java 中的 I/O 库使用装饰器模式实现了不同数据流之间的转换和增强。 让我们打开文件 a.txt,从中读取数据。InputStream 是一个抽象类,FileInputStream 是专门用于读取文件流的子类。BufferedInputStream 是一个支持缓存的数据读取类,可以提高数据读取的效率,具体代码如下: @Test public void testIO throws Exception { InputStream inputStream = new FileInputStream("C:/bbb/a.txt"); // 实现包装 inputStream = new BufferedInputStream(inputStream); byte bytes = new byte[1024]; int len; while((len = inputStream.read(bytes)) != -1){ System.out.println(new String(bytes, 0, len)); } } } } 其中 BufferedInputStream 对读取数据进行了增强。 这样看来,装饰器设计模式和代理模式似乎有点相似,接下来让我们讨论一下它们之间的区别。 第三,与代理模式的区别: 代理模式的目的是控制对对象的访问,它在对象外部提供一个代理对象来控制对原对象的访问。代理对象和原始对象通常实现相同的接口或继承相同的类,以确保两者可以相互替换。 装饰器模式的目的是动态增强对象的功能,而这是通过对象内部的包装器来实现的。在装饰器模式中,装饰器类和被装饰对象通常实现相同的接口或继承自相同的类,以确保两者可以相互替代。装饰器模式也被称为封装器模式。 在代理模式中,代理类附加了与原类无关的功能。
-
正负偏差变量 即 d2+、d2- 分别表示决策值中超出和未达到目标值的部分。而 di+、di- 均大于 0 刚性约束和目标约束(柔性目标约束有偏差) 在多目标规划中,>=/<= 在刚性约束中保持不变。当需要将约束条件转换为柔性约束条件时,需要将 >=/<= 更改为 =(因为已经有 d2+、d2- 用来表示正负偏差),并附加上 (+dii-di+) 注意这里是 +di、-di+!之所以是 +di,-di+,是因为需要将目标还原为最接近的原始刚性约束条件 优先级因素和权重因素 对多个目标进行优先排序和优先排序 目标规划的目标函数 是所有偏差变量的加权和。值得注意的是,这个加权和都取最小值。而 di+ 和 dii- 并不一定要出现在每个不同的需求层次中。具体分析需要具体问题具体分析 下面是一个例子: 题目中说设备 B 既要求充分利用,又要求尽可能不加班,那么列出的时间计量表达式即为:min z = P3 (d3- + d3 +) 使用 + 而不是 -d3 + 的原因是:正负偏差不可能同时存在,必须有 di+di=0 (因为判定值不可能同时大于目标值和小于目标值),而前面是 min,所以只要取 + 并让 di+ 和 dii- 都为正值即可。因此,得出以下规则: 最后,给出示例和相应的解法: 问题:某企业生产 A 和 B 两种产品,需要使用 A、B、C 三种设备。下表显示了与工时和设备使用限制有关的产品利润率。问该企业应如何组织生产以实现下列目标? (1) 力争利润目标不低于 1 500 美元; (2) 考虑到市场需求,A、B 两种产品的生产比例应尽量保持在 1:2; (3)设备 A 是贵重设备,严禁超时使用; (4)设备 C 可以适当加班,但要控制;设备 B 要求充分利用,但尽量不加班。 从重要性来看,设备 B 的重要性是设备 C 的三倍。 建立相应的目标规划模型并求解。 解:设企业生产 A、B 两种产品的件数分别为 x1、x2,并建立相应的目标计划模型: 以下为顺序求解法,利用 LINGO 求解: 1 级目标: 模型。 设置。 variable/1..2/:x;! s_con_num/1...4/:g,dplus,dminus;!所需软约束数量(g=dplus=dminus 数量)及相关参数; s_con(s_con_num);! s_con(s_con_num,variable):c;!软约束系数; 结束集 数据。 g=1500 0 16 15. c=200 300 2 -1 4 0 0 5; 结束数据 min=dminus(1);!第一个目标函数;!对应于 min=z 的第一小部分;! 2*x(1)+2*x(2)<12;!硬约束 @for(s_con_num(i):@sum(variable(j):c(i,j)*x(j))+dminus(i)-dplus(i)=g(i)); !使用设置完成的数据构建软约束表达式; ! !软约束表达式 @for(variable:@gin(x)); !将变量约束为整数; ! 结束 此时,第一级目标的最优值为 0,第一级偏差为 0: 第二级目标: !求 dminus(1)=0,然后求解第二级目标。 模型。 设置。 变量/1..2/:x;!设置:变量/1..2/:x; ! s_con_num/1...4/:g,dplus,dminus;!软约束数量及相关参数; s_con(s_con_num(s_con_num));! s_con(s_con_num,variable):c;! 软约束系数; s_con(s_con_num,variable):c;! 结束集 数据。 g=1500 0 16 15; c=200 300 2 -1 4 0 0 5; 结束数据 min=dminus(2)+dplus(2);!第二个目标函数 2*x(1)+2*x(2)<12;!硬约束 @for(s_con_num(i):@sum(variable(j):c(i,j)*x(j))+dminus(i)-dplus(i)=g(i)); ! 软约束表达式;! dminus(1)=0; !第一个目标结果 @for(variable:@gin(x)); ! 结束 此时,第二个目标的最优值为 0,偏差为 0: 第三目标 !求 dminus(2)=0,然后求解第三个目标。 模型。 设置。 变量/1..2/:x;!设置:变量/1..2/:x; ! s_con_num/1...4/:g,dplus,dminus;!软约束数量及相关参数; s_con(s_con_num(s_con_num));! s_con(s_con_num,variable):c;! 软约束系数; s_con(s_con_num,variable):c;! 结束集 数据。 g=1500 0 16 15; c=200 300 2 -1 4 0 0 5; 结束数据 min=3*dminus(3)+3*dplus(3)+dminus(4);!第三个目标函数。 2*x(1)+2*x(2)<12;!硬约束 @for(s_con_num(i):@sum(variable(j):c(i,j)*x(j))+dminus(i)-dplus(i)=g(i)); ! 软约束表达式;! dminus(1)=0; !第一个目标约束条件; ! dminus(2)+dplus(2)=0; !第二个目标约束条件 @for(variable:@gin(x));! 结束 最终结果为 x1=2,x2=4,dplus(1)=100,最优利润为
-
使用 Git 进行版本控制
-
如何使用 splitChunks 对代码分割进行精细控制
-
使用 gitee 和 git 进行链接
-
使用 alembic 进行数据库版本控制
-
Git 版本控制用户手册 - 6 - 将本地项目提交到空白版本库