03_Git分支管理

797 阅读4分钟

一般来说,我们在进行开发时,往往分成两个分支:main分支和develop分支。

其中main分支是已经合并的代码,develop分支是正在开发的分支。

分支概念:

main分支和其他分支的关系更像是一个宇宙和另外平行宇宙的关系。

  • 互不影响,但又有联系。

举一个场景例子:

  1. 工作室有一个邮箱项目已经上线并在使用中,我们把它的版本称为1.0。
  2. 过了几天,项目经理提了个新需求,在1.0的基础上开发1.1版本,新增功能。
  3. 为了不影响原先1.0版本的运行,我们在存放main分支的基础上,开了新的分支develop分支。
  4. 新功能就在develop分支上开发,main分支正常运行。
  5. 过了几天新功能开发好了,然后我们进行了代码合并。将develop分支上的新功能(代码)通过git审查对比无误之后合并到了main分支上(合并的过程我们暂且理解为维护),由此邮箱1.1版本诞生了。

Github之pull requests(PRs)

01-Git初体验中的演示为例,可视化演示。

github上的pull requests操作(PRs)

prs是一个可视化的合并代码操作。

它提供的功能包括但不仅限于:可视化操作、代码审查、继承测试、文档化和评论以及合并功能。在团队开发中尤为重要。

  1. 新建develop分支

我们可以得到和main分支内容一摸一样的新分支。

  1. 本地切换到develop分支

本地分支和远程分支名字一样,是同一个内容,区别就是一个在本地,另外一个在远程仓库。

  使用 git branch 查看本地分支

  使用 git branch -r 查看远程分支

  使用 git branch -a 查看所有分支

切换到develop分支

  1. 修改代码

  1. 放入暂存、提交、提交至远程仓库

  1. 提交拉去请求
  • 可以看到两个分支内容是不一样的

  • 拉请求

按顺序点击就行,当git审查通过后就可以合并代码

  • 完成合并,可到code选项查看,此时main分支已经更新和deve;op分支一样内容。

Git之merge

merge也是合并操作,但更多是个人,用于本地。

场景:

  • 团队开发,main分支和A B两个开发分支。你在A分支开发。
  • 昨天,B分支的同事开发完了相对应的功能,并且完成了PR,现在他的代码已经在main分支中了。
  • 今天,你需要main分支上已经开发的接着进行开发。(main分支和你的A分支内容不一样了,你的落后了)
  • 你想用pull requests同样,将main分支代码合并到你的分支上,然后你在拉去。
  • 但仓库管理员拒绝了你的想法:一般晚上下班时才会pull requests。
  • 这时你该怎么做呢?

于是,merge就派上了用场。

你需要操作是:

  1. 查看远程分支:
git branch -r
  1. 然后,从main分支拉取最新的代码到A分支(你的分支),你需要你先切换到mian分支,拉取最新的代码,再切换回A分支,最后将main分支的更新合并到A分支上。
# 切换到main分支。
git checkout main

# 拉去最新的main分支代码
git pull origin main

# 切换回A分支
git checkout web

# 将main分支的更新合并到A分支
git merge main

以下是一个真实示例过程:

minor cell@MinorCell MINGW64 /f/IT/Works/CP-EmailTools (webToolsBranch)
$ git branch -r
  origin/HEAD -> origin/main
  origin/googleToolsBranch
  origin/main
  origin/webToolsBranch

minor cell@MinorCell MINGW64 /f/IT/Works/CP-EmailTools (webToolsBranch)
$ git checkout main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

minor cell@MinorCell MINGW64 /f/IT/Works/CP-EmailTools (main)
$ git pull origin main
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 22 (delta 4), reused 19 (delta 2), pack-reused 0
Unpacking objects: 100% (22/22), 30.30 KiB | 159.00 KiB/s, done.
From https://github.com/CodePaintStudio/CP-EmailTools
 * branch            main       -> FETCH_HEAD
   4a0ecd8..1e310c8  main       -> origin/main
Merge made by the 'ort' strategy.
 WebTools/index.html             |    2 +-
 backfront/.gitignore            |    1 +
 backfront/app.js                |   23 +
 backfront/demo/axios.min.js     |    2 +
 backfront/demo/index.html       |  131 +++++
 backfront/flow.drawio           |  159 +++++
 backfront/handle/email.js       |  170 ++++++
 backfront/package-lock.json     | 1228 +++++++++++++++++++++++++++++++++++++++
 backfront/package.json          |    7 +
 backfront/readme.md             |   19 +
 backfront/router/emailServer.js |   95 +++
 11 files changed, 1836 insertions(+), 1 deletion(-)
 create mode 100644 backfront/.gitignore
 create mode 100644 backfront/app.js
 create mode 100644 backfront/demo/axios.min.js
 create mode 100644 backfront/demo/index.html
 create mode 100644 backfront/flow.drawio
 create mode 100644 backfront/handle/email.js
 create mode 100644 backfront/package-lock.json
 create mode 100644 backfront/package.json
 create mode 100644 backfront/readme.md
 create mode 100644 backfront/router/emailServer.js

minor cell@MinorCell MINGW64 /f/IT/Works/CP-EmailTools (main)
$ git checkout webToolsBranch 
Switched to branch 'webToolsBranch'
Your branch is up to date with 'origin/webToolsBranch'.

minor cell@MinorCell MINGW64 /f/IT/Works/CP-EmailTools (webToolsBranch)
$ git merge main
Updating 140329c..9eb0aed
Fast-forward
 WebTools/pnpm-lock.yaml         |   36 --
 backfront/.gitignore            |    1 +
 backfront/app.js                |   23 +
 backfront/demo/axios.min.js     |    2 +
 backfront/demo/index.html       |  131 +++++
 backfront/flow.drawio           |  159 +++++
 backfront/handle/email.js       |  170 ++++++
 backfront/package-lock.json     | 1228 +++++++++++++++++++++++++++++++++++++++
 backfront/package.json          |    7 +
 backfront/readme.md             |   19 +
 backfront/router/emailServer.js |   95 +++
 11 files changed, 1835 insertions(+), 36 deletions(-)
 create mode 100644 backfront/.gitignore
 create mode 100644 backfront/app.js
 create mode 100644 backfront/demo/axios.min.js
 create mode 100644 backfront/demo/index.html
 create mode 100644 backfront/flow.drawio
 create mode 100644 backfront/handle/email.js
 create mode 100644 backfront/package-lock.json
 create mode 100644 backfront/package.json
 create mode 100644 backfront/readme.md
 create mode 100644 backfront/router/emailServer.js