LOADING

加载过慢请开启缓存 浏览器默认开启

基于阿里云云效Codeup的Git代码管理

基于阿里云云效Codeup的Git代码管理(基于Windows 11)


基本要求

  • 理解并掌握代码版本管理工具Git的基本原理与常用命令
  • 理解并掌握Git代码分支管理与冲突处理的基本原则与方法
  • 了解并掌握阿里云云效代码管理Codeup的核心功能
  • 熟练掌握基于Codeup建立远程与本地仓库并开展代码管理的基本方法

实验过程

环境搭建

本地安装Git与Python3
  • 安装Git与Python3:(略)
  • 查看python版本:
python --version

屏幕截图 2024-10-21 184006

  • 查看Git版本:
git --version

屏幕截图 2024-10-21 165003

  • 配置Git用户名与电子邮箱
git config --global user.name "<username>"
git config --global user.email "<email>"

屏幕截图 2024-10-21 170933

  • 安装Python3虚拟环境(使用Anaconda)

打开Anaconda Prompt,输入命令:

conda create -n <name> python=3.10

回车开始创建。

屏幕截图 2024-10-21 184324

弹出询问命令,输入y回车

屏幕截图 2024-10-21 184757

创建完成。

本地安装Python第三方库
  • 激活虚拟环境

安装第三方库前,首先需要激活虚拟环境。

conda activate 环境名

屏幕截图 2024-10-21 185927

  • 设置独立依赖库

Python虚拟环境可以实现为不同项目设置独立的依赖库,以实现在统一系统中不同项目所需软件包的隔离。

配置requirements.txt文件,提供要安装的Python库名,如web服务框架Sanic,http基础库Requests以及用于访问数据库的基础库PyMySQL,文件示例如下:

// requirements.txt
sanic
requests
pymysql

根据requirements.txt文件安装第三方库

(base) C:\Users\14043>cd /d D:\MashineLearning\envs		//切换到文本文件所在目录

(base) D:\MashineLearning\envs>conda activate pyms_venv		//激活虚拟环境并切换到目标环境下

(pyms_venv) D:\MashineLearning\envs>pip install -r requirements.txt		//以文件为基础安装环境

屏幕截图 2024-10-21 191434

以上情况即为安装已完成

基于阿里云云效代码管理Codeup创建代码库

新建远程仓库

屏幕截图 2024-10-24 103334

  • 新建代码库”exp_pyms“,新建时注意勾选“创建.gitignore”,选择Python模板。Codeup会默认创建Master分支,并提供推荐.gitignore文件。屏幕截图 2024-10-24 103551
克隆远程仓库至本地仓库(HTTPS)
  • 配置HTTPS克隆密码

进入Codeup系统,在右上角“个人设置”中进入“HTTPS密码”页面。

屏幕截图 2024-10-24 104110

  • 复制代码库地址(HTTPS URL)

进入代码库exp_pyms的首页,找到其HTTPS地址,复制URL,用作仓库地址。记为repo_addr

屏幕截图 2024-10-24 104410

进入本地目录,使用git clone克隆远程仓库。

git clone <repo_addr>

执行时需要在弹出的窗口内正确输入远程仓库的用户名和密码完成身份验证。

屏幕截图 2024-10-24 105712

[!NOTE]

如果之前登陆过其他账号,此处可能出现:

屏幕截图 2024-10-24 105900

这时可以通过清除原有的用户名和密码的方式重新登陆。

 git config --global user.name ""
 git config --global user.email ""

bash返回下面语句则说明clone完成:

libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

本操作会把目录exp_pyms克隆到本地,其中包含:

  1. 一个隐藏目录.git,为Git仓库核心配置
  2. .gitignore文件
  3. 主分支Master中全部文件的工作副本(此时只有.gitignore

后续操作均在目录exp_pyms中进行。

配置本地仓库

README.md文件为例说明如何添加文件到本地仓库:

MINGW64 /e/exp_pyms (master)
touch README.md			//创建空白文件

git add README.md		//将其添加到Git暂存区

git status				//查看当前工作目录中各文件状态
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   README.md
同步本地仓库至远程仓库

执行以下操作,完成后查看远程仓库,已出现本次提交的README.md文件。

#提交前应养成习惯,先从远程仓库pull最新代码至本地进行冲突检查。
$ git pull
Already up to date.

$ git commit -m "docs:创建README.md"		//将全部暂存文件提交至本地仓库
[master 6d2d026] docs:创建README.md
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
 
 $ git push origin master			//推送本地提交至origin/master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 20 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://codeup.aliyun.com/.../exp_pyms.git
   5cc9ea6..6d2d026  master -> master

屏幕截图 2024-10-24 114054

屏幕截图 2024-10-24 114134

其中,推送时使用的远程仓库origin与分支master是在.git/config文件中定义的。

使用SSH访问远程仓库

本地执行涉及访问远程仓库的命令时,如使用HTTPS方式访问,每次均需提供用户名和密码,如果本地开发环境可信,可使用SSH方式访问远程仓库,提高效率。

生成SSH公私钥对

使用命令ssh-keygen新建SSH公私钥对。主要参数:

  1. -t 指定密钥类型,如ed25519rsa
  2. -f 指定用于存储私钥的文件名,不使用则提示默认文件及路径~/.ssh/id_rsa
  3. -C 指定注释内容,可用作公钥名称,Codeup称为“标题”
$ ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -C "<注释内容>"
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/14043/.ssh/id_ed25519
Your public key has been saved in /c/Users/14043/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:... xxx@xxx
The key's randomart image is:
//略

屏幕截图 2024-10-24 144621

创建成功后,可在本地~/.ssh/目录查看到新建的公钥和密钥文件。

配置远程仓库所需公钥
  • 在本地使用cat命令查看公钥
$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3N... ...@...

屏幕截图 2024-10-24 145559

  • 添加公钥

通过“个人设置->SSH公钥”粘贴该公钥,完成添加。

屏幕截图 2024-10-24 145806

重建远程仓库并重新克隆

删除并重建云端远程仓库exp_pyms,并删除本地工作目录,重新使用SSH方式克隆代码库。

选择地址时,要选取SSH方式的地址:

屏幕截图 2024-10-24 150630

$ git clone git@codeup.aliyun.com:.../exp_pyms.git
Cloning into 'exp_pyms'...
The authenticity of host 'codeup.aliyun.com (118.31.165.50)' can't be established.
RSA key fingerprint is SHA256:...
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'codeup.aliyun.com' (RSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

屏幕截图 2024-10-24 150759

代码托管练习

在本地工作目录exp_pyms进行Git基础练习

创建文件并同步至远程仓库
  • 创建test.py,暂存至暂存区,观察其状态。
$ echo "import json" > test.py			//创建代码文件

$ git add test.py					//暂存代码文件
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it

$ git status
        //观察文件状态,已变更为被跟踪(Tracked),且已暂存(Staged),即等待被提交。
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.py

屏幕截图 2024-10-24 151922

  • 提交至本地仓库,观察文件状态。
$ git commit -m "feat(test.py):引入json"
[master 74af60b] feat(test.py):引入json
 1 file changed, 1 insertion(+)
 create mode 100644 test.py

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

屏幕截图 2024-10-24 152207

  • 推送至远程仓库,观察其状态。
$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 20 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 297 bytes | 297.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
   4192f8a..74af60b  master -> master
   
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

屏幕截图 2024-10-24 152503

至此,新建文件、暂存、提交、推送等基本操作均已完成,并将本地代码同步托管到了云端Codeup。

修改文件并同步至远程仓库
  • 修订test.py后观察文件状态
$ echo "import random" > test.py

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.py

no changes added to commit (use "git add" and/or "git commit -a")

屏幕截图 2024-10-24 152855

此时test.py在工作目录中的状态是被跟踪,但是修改暂未被暂存,必须再次使用git add命令咱村该文件,方可再次提交。

$ git add test.py
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.py

屏幕截图 2024-10-24 153157

  • 修订test.py

新增一条语句后,再次观察文件状态,可见test.py同时具有等待被提交以及尚未暂存的内容,显然刚添加的语句属于尚未被暂存的内容。

$ echo "import re" >> test.py

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.py

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.py

屏幕截图 2024-10-24 153533

为了简化操作,可使用git commit命令并应用-a参数直接提交。应用-a时,Git会自动暂存被修改或删除的文件(未被跟踪的文件除外)

$ git commit -a -m "feat(test.py):引入random"
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it
[master cbc5ec9] feat(test.py):引入random
 1 file changed, 2 insertions(+), 1 deletion(-)

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

屏幕截图 2024-10-24 153811

如果确认可推送,可直接使用git push简化操作。

$ git push	// 在当前本地master分支使用等价于git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 305 bytes | 305.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
   74af60b..cbc5ec9  master -> master

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

屏幕截图 2024-10-24 163620

基于提交历史比较版本差异

git log命令可用于查看提交历史记录,默认输出按时间顺序列出所有的提交,最近的更新排在最上面。输出内容中,commit后紧跟本次提交的SHA-1校验和,可作为提交的版本号。

$ git log
commit cbc5ec9089bc2d0d0980e97a08edf744f00520a6 (HEAD -> master, origin/master, origin/HEAD)
Author: “” <“”>
Date:   Thu Oct 24 15:37:13 2024 +0800

    feat(test.py):引入random

commit 74af60b91cf76652b81cb29016d74532b0f32c7d
Author: “” <“”>
Date:   Thu Oct 24 15:17:20 2024 +0800

    feat(test.py):引入json

commit 4192f8aa41d98aadeffbcdb6234aa8d5a6f4a776
Author: ... <...@xx.com>
Date:   Thu Oct 24 15:04:24 2024 +0800

    Initial commit

屏幕截图 2024-10-24 163659

使用-p--patch参数查看每次提交所引入的差异,并在其后指定要查看的提交数,如-1说明只查看最近1次提交。

$ git log -p -1
commit cbc5ec9089bc2d0d0980e97a08edf744f00520a6 (HEAD -> master, origin/master, origin/HEAD)
Author: “” <“”>
Date:   Thu Oct 24 15:37:13 2024 +0800

    feat(test.py):引入random

diff --git a/test.py b/test.py
index 0349a44..3708c1c 100644
--- a/test.py
+++ b/test.py
@@ -1 +1,2 @@
-import json
+import random
+import re

屏幕截图 2024-10-24 163805

要将当前版本与过往提交的某个版本进行比较时,可在git log查询的基础上使用git diff查询某过往提交版本与当前工作目录中版本的差异。

$ echo "print('Hello,world');" >> test.py 		//修订代码文件

$ git diff cbc5ec9089bc2d0d0980e97a08edf744f00520a6 test.py
        //将当前工作目录版本与SHA-1校验和为"cbc5ec..."的历史提交比较
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it
diff --git a/test.py b/test.py
index 3708c1c..3c946e8 100644
--- a/test.py
+++ b/test.py
@@ -1,2 +1,3 @@
 import random
 import re
+print('Hello,world');

屏幕截图 2024-10-24 164346

按需撤销暂存、提交或推送
  • 撤销暂存

查看文件状态,确认test.py修订未暂存,随后执行暂存并再次查看文件状态。

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.py

no changes added to commit (use "git add" and/or "git commit -a")

$ git add test.py
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.py

屏幕截图 2024-10-24 164900

可见系统提示(use "git restore --staged <file>..." to unstage),应用后观察文件状态,发现test.py状态恢复为尚未暂存。

$ git restore --staged test.py 	//将提交区(代码仓库)中HEAD指向的版本复制到暂存区

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.py

no changes added to commit (use "git add" and/or "git commit -a")

屏幕截图 2024-10-24 165843

也可以使用git reset HEAD <file>来取消对test.py的暂存。

[!WARNING]

若该指令重复执行,将会依次取消对test.py的历史提交,是非常危险的命令,慎用。

$ git add test.py
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it

$ git reset HEAD test.py
Unstaged changes after reset:
M       test.py

屏幕截图 2024-10-24 170259

  • 撤销提交

提交当前包含打印Hello,world语句的test.py,并查看文件状态。

$ git commit -a -m "feat(test.py):打印Helloworld"
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it
[master ca8a0ed] feat(test.py):打印Helloworld
 1 file changed, 1 insertion(+)

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

屏幕截图 2024-10-24 170530

使用命令git reset HEAD~撤销提交后查看文件状态,发现test.py已恢复到尚未暂存与提交的状态。

[!IMPORTANT]

git reset HEAD~若重复执行,HEAD会不断偏移,慎用。

$ git reset HEAD~	
    //HEAD~是HEAD的父节点,该命令设置HEAD指针指向当前提交(末次提交)的上一次提交。
Unstaged changes after reset:
M       test.py

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.py

no changes added to commit (use "git add" and/or "git commit -a")

屏幕截图 2024-10-24 171139

  • 撤销推送

本地回滚HEAD后强制推送远程仓库,等价于撤销先前推送。

$ git commit -a -m "feat(test.py):打印Helloworld"		//本地提交
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it
[master 593e0de] feat(test.py):打印Helloworld
 1 file changed, 1 insertion(+)

$ git push origin master		//推送远程
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 330 bytes | 330.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
   cbc5ec9..593e0de  master -> master

屏幕截图 2024-10-24 175143

执行以上操作之后登录Codeup,查看远程仓库master分支“提交”页面,可见本次新提交记录。

$ git reset HEAD~
Unstaged changes after reset:
M       test.py

$ git push -f origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
 + 593e0de...cbc5ec9 master -> master (forced update)

屏幕截图 2024-10-24 183845

执行以上操作后登录Codeup,查看远程仓库master分支“提交”页面,可见上次推送对应的“提交”已经消失。

屏幕截图 2024-10-24 184052

[!WARNING]

git push -f会强行推送,并抹去上次推送的全部内容,慎用。

删除文件以及恢复指定文件
  • 保留文件,取消跟踪
$ git rm --cached test.py	//取消对该文件的跟踪
rm 'test.py'

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    test.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.py

$ git add test.py		//恢复对该文件的跟踪
warning: in the working copy of 'test.py', LF will be replaced by CRLF the next time Git touches it

屏幕截图 2024-10-26 094231

  • 从暂存区域恢复文件

修订文件,暂存,随后删除文件但并不暂存删除操作,可将暂存区域index暂存的版本恢复到工作目录。

$ git diff test.py		//前面增加了打印Helloworld的语句,尚未提交,通过diff查看确认

$ git status		//观察文件状态

$ git add test.py		//将修改内容暂存
$ git status			//观察文件状态
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.py

$ rm test.py		//使用rm删除
$ cat test.py		//查看test.py已不存在
cat: test.py: No such file or directory

$ git status		//观察文件状态
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.py

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    test.py

$ git restore test.py		//从暂存区恢复test.py,即将暂存区版本复制到工作目录
$ cat test.py			//查看test.py,包含打印Helloworld语句
import random
import re
print('Hello,world');

$ git status		//观察文件状态,也恢复为等待提交
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.py

屏幕截图 2024-10-26 095254

屏幕截图 2024-10-26 095301

  • 从本地仓库恢复文件

修订文件,暂存,随后删除文件且同时暂存删除操作,则先前暂存但未提交的修订内容丢失,仅可从本地仓库HEAD恢复。

$ git rm test.py	//使用命令rm删除文件后“立即暂存”删除状态,等同于git rm <file>
error: the following file has changes staged in the index:
    test.py
(use --cached to keep the file, or -f to force removal)

$ git rm -f test.py		//对于已暂存修改的文件,使用git rm删除必须带上参数-f
rm 'test.py'

$ git status		
//观察文件状态,test.py已标注为deleted,先前暂存的modified消失,此时暂存区已无该文件
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    test.py

$ git restore test.py		//执行restore不能恢复被删除的文件
error: pathspec 'test.py' did not match any file(s) known to git

$ git restore --staged test.py		//将版本库(提交区)中HEAD指向的版本复制到暂存区

$ cat test.py		//查看test.py并未恢复
cat: test.py: No such file or directory

$ git status		//观察文件状态
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    test.py

no changes added to commit (use "git add" and/or "git commit -a")

$ git restore test.py		//从暂存区恢复test.py,即将暂存区的版本复制到工作目录

$ cat test.py		查看test.py,不包含打印Helloworld语句
import random
import re

屏幕截图 2024-10-26 100626

也可以使用单条命令,基于提交区HEAD指向的版本,同时恢复暂存区与工作区中的test.py

git restore --source=HEAD --staged --worktree test.py		//等价于git checkout HEAD test.py
  • 从远程仓库恢复文件

若删除的文件已无法从本地仓库恢复,则需考虑从远程仓库恢复文件。

git fetch origin会抓取克隆(或上一次抓取)后新推送的所有工作。

[!NOTE]

git fetch只会将数据下载到本地仓库,而不会自动合并或修改当前工作区的文件,后续需要手动融合

运行git pull则会首先自动抓取远程分支的所有工作,并将其融合到当前分支。具体来说:

  1. 从远程仓库下载最新的提交历史和代码文件到本地;
  2. 若本地有修改尚未提交,会尝试将远程代码和本地代码自动合并;
  3. 若合并过程中出现冲突,需用户手动解决冲突,提交合并结果。
$ git pull

以上指令对多人协作情况十分重要。

分支管理练习

Git的分支是指在版本控制下独立开发的代码线。每个仓库可有一至多个分支,每个分支可以包含不同的代码修改和提交历史。

分支的好处:

  1. 并行开发:可以在不同分支上同时进行独立开发
  2. 特性开发:可以在单独分支上开发新功能,完成后合并到主分支
  3. Bug修复:可以在一个分支上修复错误,将修复内容合并到主分支,而不影响正在进行的其他开发工作
  4. 版本管理:每个分支都有自己的提交历史,可以方便地在不同版本之间切换和查看
创建develop分支用于开发
  • 新建并切换到develop分支:
$ git branch develop	//本质上是在当前所在提交对象上新建指针

$ git switch develop
Switched to branch 'develop'

也可以使用git checkout -b <branch>达到同样效果

[!IMPORTANT]

创建一个分支,本质是创建了一个指向当前提交对象的指针

  • 创建文件config.json,仅供本地测试使用,需要通过.gitignore忽略
{
    "dev_sn": "68h89asdf79fxfa",
    "dev_pwd": "hda89dsa0sa9dha"
}
  • 创建代码文件,其中包含需要Git托管的业务逻辑代码。
import json

def get_config(path: str) -> dict:
    with open(path, "r") as f:
        return json.load(f)

if __name__ == '__main__':
    config: dict = 
get_config("./config.json")
    print(f"dev_sn: {config['dev_sn']}")
    print(f"dev_pwd: {config['dev_pwd']}")
  • 本地测试python脚本。
//在文件所在目录打开
python xxx.py

屏幕截图 2024-10-26 111029

  • 配置忽略config.json,即将config.json写入.gitignore文件。
$ echo "config.json" >> .gitignore
  • 暂存,提交,推送。完成以下操作后,在Codeup查看develop分支,可发现test.py已经成功推送到远程仓库。
$ git add xxx.py .gitignore

$ git status
On branch develop
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   .gitignore
        new file:   xxx.py

$ git commit -m "feat(xxx.py):读取config.json,打印dev_sn与dev_pwd;docs(.gitignore):添加config.json"
[develop c235300] feat(xxx.py):读取config.json,打印dev_sn与dev_pwd;docs(.gitignore):添加config.json
 2 files changed, 12 insertions(+)
 create mode 100644 xxx.py
 
$ git status
On branch develop
nothing to commit, working tree clean

$ git push origin develop
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 20 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 598 bytes | 598.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
 * [new branch]      develop -> develop

屏幕截图 2024-10-26 110403

创建feature分支用于特定功能开发
  • 从当前分支创建feature分支并推送当前暂存文件至远程仓库对应分支。
$ git checkout -b feature
Switched to a new branch 'feature'

屏幕截图 2024-10-26 110625

  • 修订xxx.py,完成feature功能要求,从config.json读取并打印login_url
import json

def get_config(path: str) -> dict:
    with open(path, "r") as f:
        return json.load(f)

if __name__ == '__main__':
    config: dict = get_config("./config.json")
    print(f"dev_sn: {config['dev_sn']}")
    print(f"dev_pwd: {config['dev_pwd']}")
    print(f"login_url: {config['login_url']}")
  • 修订config.json,对应增补login_url,用于xxx.py本地测试。
{
    "dev_sn": "68h89asdf79fxfa",
    "dev_pwd": "hda89dsa0sa9dha",
    "login_url": "https://test.cn/login"
}
  • 本地测试,确认feature开发情况满足要求。
//在文件所在目录打开
python xxx.py

屏幕截图 2024-10-26 111422

  • 本地暂存提交,推送远程仓库feature分支。
$ git commit -a -m "feat(xxx.py):打印login_url"
[feature 16bea33] feat(xxx.py):打印login_url
 1 file changed, 3 insertions(+), 3 deletions(-)

$ git push origin feature
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 411 bytes | 411.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
 * [new branch]      feature -> feature

屏幕截图 2024-10-26 112914

  • feature分支合并到develop分支,并推送远程仓库
$ git checkout develop
Switched to branch 'develop'

$ cat xxx.py	//此时尚未添加打印login_url的语句
$ git merge feature
Updating c235300..16bea33
Fast-forward
 xxx.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

$ cat xxx.py	//此时develop分支下的代码文件已存在打印login_url的语句
import json

def get_config(path: str) -> dict:
        with open(path, "r") as f:
                return json.load(f)

if __name__ == '__main__':
        config: dict = get_config("./config.json")
        print(f"dev_sn: {config['dev_sn']}")
        print(f"dev_pwd: {config['dev_pwd']}")
        print(f"login_url: {config['login_url']}")

$ git push origin develop
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
   c235300..16bea33  develop -> develop

屏幕截图 2024-10-26 113507

此时查看Codeup,在exp_pyms项目的develop分支,可发现代码已提交成功。

屏幕截图 2024-10-26 113810

如开发完成,可继续将develop分支合并到master分支并推送远程仓库。若云端master有对应生产环境流水线,便会触发流水线执行打包部署。

$ git switch master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git merge develop
Updating cbc5ec9..16bea33
Fast-forward
 .gitignore          |  1 +
 xxx.py | 11 +++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 xxx.py

$ git push origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
   cbc5ec9..16bea33  master -> master

屏幕截图 2024-10-26 114201

创建hotfix分支执行热修复

代码在master上线后,若突然发现严重bug,则应马上修复,可立刻从master分支迁出一个hotfix分支用于热修复。

$ git checkout -b hotfix
Switched to a new branch 'hotfix'

屏幕截图 2024-10-26 114601

简单设定修复措施,即删除xxx.py中打印dev_pwd的语句。

完成修订及测试后,将hotfix分支合并到master分支和develop分支。

$ git commit -a -m "fix(xxx.py):不应打印密码,删除对应语句"
[hotfix adcd281] fix(xxx.py):不应打印密码,删除对应语句
 1 file changed, 1 deletion(-)

$ git checkout master	//切换到分支master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git merge hotfix		//在master合并hotfix
Updating 16bea33..adcd281
Fast-forward
 xxx.py | 1 -
 1 file changed, 1 deletion(-)

$ cat xxx.py	//此时打印dev_pwd的语句已删除
import json

def get_config(path: str) -> dict:
        with open(path, "r") as f:
                return json.load(f)

if __name__ == '__main__':
        config: dict = get_config("./config.json")
        print(f"dev_sn: {config['dev_sn']}")
        print(f"login_url: {config['login_url']}")

$ git push origin master	//推送远程仓库分支master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 403 bytes | 403.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
   16bea33..adcd281  master -> master

$ git checkout develop		//切换到分支develop
Switched to branch 'develop'

$ git merge hotfix		在分支develop合并hotfix
Updating 16bea33..adcd281
Fast-forward
 xxx.py | 1 -
 1 file changed, 1 deletion(-)

$ git push origin develop		//推送远程仓库分支develop
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To codeup.aliyun.com:.../exp_pyms.git
   16bea33..adcd281  develop -> develop

屏幕截图 2024-10-26 115119

最后删除hotfix分支。

$ git branch -d hotfix
Deleted branch hotfix (was adcd281).

屏幕截图 2024-10-26 115513

代码冲突处理练习(暂略)