Git基础(2)Git流程简介

(一)Git规范

1、Git 功能点及命名规范

(1)主干分支,命名为 master,用作发布环境,上面的每一次提交都是可以发布的。永远是可用的稳定版本,不能直接在该分支上开发。

(2)开发主分支,命名为 developer,这个分支对应的是集成测试环境。所有新功能以这个分支来创建自己的开发分支,该分支只做合并操作,不能直接在该分支上进行开发。一旦功能开发完成,就向 developer 分支合并,合并完成后,删除功能分支。

(3)功能开发分支,命名为 feature/xxx,在develop上创建分支,以自己开发功能模块命名,功能测试正常后合并到develop分支。

(4)功能bug修复分支,命名为 feature/xxx/fix,feature分支合并之后发现bug,在develop上创建分支进行修复,之后合并回develop分支。

(5)紧急修复分支,命名为 hotfix/xxx ,用于处理生产线上代码的 Bug-fix,每个线上代码的 Bug-fix 都需要开一个 hotfix 分支。完成后,向 developer 分支和 master 分支上合并。合并完成后,删除 hotfix 分支。

(6)发布分支,命名为 release ,当 developer 分支测试达到可以发布状态时,开出一个 release 分支来,然后做发布前的准备工作。这个分支对应的是预发环境。之所以需要这个 release 分支,是我们的开发可以继续向前,不会因为要发布而被 block 住而不能提交。一旦 release 分支上的代码达到可以上线的状态,那么需要把 Release 分支向 master 分支和developer 分支同时合并,以保证代码的一致性。然后再把 release 分支删除掉。

2、基本流程:从开发到版本发布

(1)首先有一个 master 版本,如果新开发一个功能,则会从master分支上创建一个 develop 分支

(2)想开发新功能,从develop分支上,分别创建对应的功能分支feature/xxx。

(3)如果线上跑的程序出现bug了,从master分支上,创建一个热修复分支 hotfix 分支 。修复bug以后,尽快合并到master中,并且打上tag。同时修复的版本,也要合并到 develop 分支上

(4) 功能分支feature/xxx完成后,合并到 develop 分支,然后基于 develop分支 创建预发布分支(release) ,在设个分支上进行上线前的测试。

(5)如果 release 分支出现bug,则直接在 release 分支上进行发布,修复以后,合并到master分支上,打上tag。开发分支也需要这个功能,也要合并到开发分支上。

(6)hotfix 分支 修复完bug 后,直接就删除了。

(二)Git 基本操作

————————————   start   ——————————————————

1、在github 或者 gitee 建立一个仓库git clone

(base) never@Wonder my_github % git clone git@gitee.com:changfengwangbao/test-git.git

正克隆到 'test-git'...

remote: Enumerating objects: 37, done.

remote: Counting objects: 100% (37/37), done.

remote: Compressing objects: 100% (29/29), done.

remote: Total 37 (delta 13), reused 0 (delta 0), pack-reused 0

接收对象中: 100% (37/37), 5.10 KiB | 5.10 MiB/s, 完成.

处理 delta 中: 100% (13/13), 完成.

——————————————————————————————————

2、到本地建立dev分支:

(base) never@Wonder my_git % git checkout -b dev

切换到一个新分支 ‘dev'

——————————————————————————————————

3、在dev下增加hello.c 文件

(base) never@Wonder my_git % touch hello.c

——————————————————————————————————

4、将 hello.c 添加本地仓库推送远程dev

(base) never@Wonder my_git % git push -u origin “dev”

枚举对象中: 16, 完成.

对象计数中: 100% (16/16), 完成.

使用 8 个线程进行压缩

压缩对象中: 100% (10/10), 完成.

写入对象中: 100% (16/16), 1.28 KiB | 1.28 MiB/s, 完成.

总共 16(差异 3),复用 0(差异 0),包复用 0

remote: Powered by GITEE.COM [GNK-6.4]

remote: Create a pull request for 'dev' on Gitee by visiting:

remote:     https://gitee.com/changfengwangbao/test-git/pull/new/changfengwangbao:dev...changfengwangbao:master

To https://gitee.com/changfengwangbao/test-git.git

* [new branch]      dev -> dev

分支 'dev' 设置为跟踪 'origin/dev'。

——————————————————————————————————

5、使用git branch –vv 查看本地dev是否和远程建立连接

(base) never@Wonder my_git % git branch -vv

* dev        c55d1b3 [origin/dev] my_new is a text

  master     c55d1b3 [origin/master] my_new is a text

  new_branch 308a0f5 switch my branch

(base) never@Wonder my_git % git push

致命错误:当前分支 release 没有对应的上游分支。

——————————————————————————————————

6、如果没有则使用git branch –setup-stream 将本地分支和远程建立关联(注意如果没有建立关联对本地的git push是不成功或者每次都需要 git push指定远程分支)

备注:需要取消 gitee【设置】中【邮箱管理】的【禁用命令行推送】选项

(base) never@Wonder my_git % git push --set-upstream origin release

枚举对象中: 10, 完成.

对象计数中: 100% (10/10), 完成.

使用 8 个线程进行压缩

压缩对象中: 100% (5/5), 完成.

写入对象中: 100% (6/6), 602 字节 | 602.00 KiB/s, 完成.

总共 6(差异 2),复用 0(差异 0),包复用 0

remote: Powered by GITEE.COM [GNK-6.4]

remote: Create a pull request for 'release' on Gitee by visiting:

remote:     https://gitee.com/changfengwangbao/test-git/pull/new/changfengwangbao:release...changfengwangbao:master

To https://gitee.com/changfengwangbao/test-git.git

* [new branch]      release -> release

分支 'release' 设置为跟踪 ‘origin/release'。

——————————————————————————————————

7、创建并切换到 feature/ 分支

(base) never@Wonder my_git % git checkout -b feature/bookshop

切换到一个新分支 ‘feature/bookshop'

——————————————————————————————————

8、对hello.c进行编辑提交到本地仓库

(base) never@Wonder my_git % vi hello.c

(base) never@Wonder my_git % git add hello.c

(base) never@Wonder my_git % git commit hello.c

[feature/bookshop 755249b] test git commit

1 file changed, 1 insertion(+)

create mode 100644 hello.c

——————————————————————————————————

9、使用git checkout dev 切到 dev分支

(base) never@Wonder my_git % git checkout dev

D a.txt

D b.txt

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

切换到分支 'dev'

您的分支与上游分支 'origin/dev' 一致。

——————————————————————————————————

10、使用 git merge 合并 把 feature分支内容合并到dev

(base) never@Wonder my_git % git merge feature/bookshop

更新 c55d1b3..755249b

Fast-forward

hello.c | 1 +

1 file changed, 1 insertion(+)

create mode 100644 hello.c

——————————————————————————————————

11、 从 dev checkout 一个 realease分支用于预发布测试

(base) never@Wonder my_git % git checkout -b release

切换到一个新分支 ‘release'

——————————————————————————————————

12、假设测试中发现问题 对release分支checkout 一个 fix/xx 分支

(base) never@Wonder my_git % git checkout -b fix/hello

切换到一个新分支 ‘fix/hello'

——————————————————————————————————

13、修改hello.c 然后 合并到 release

(base) never@Wonder my_git % vi hello.c

(base) never@Wonder my_git % git checkout release

D a.txt

D b.txt

M hello.c

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

切换到分支 'release'

(base) never@Wonder my_git % git merge fix/hello

已经是最新的。

——————————————————————————————————

14、假定满足发布条件则checkout到master 将release分支合并到master

(base) never@Wonder my_git % git checkout master

D a.txt

D b.txt

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

切换到分支 'master'

您的分支与上游分支 'origin/master' 一致。

(base) never@Wonder my_git % git merge release

更新 c55d1b3..7c1b37f

Fast-forward

hello.c | 2 ++

1 file changed, 2 insertions(+)

create mode 100644 hello.c

——————————————————————————————————

15、切换到master对其建立tag

(base) never@Wonder my_git % git tag -a v1.0-release -m "全新版本,增加了细节信 息的展示功能”

——————————————————————————————————

16、将release合并到dev

(base) never@Wonder my_git % git checkout dev  

D a.txt

D b.txt

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

切换到分支 'dev'

(base) never@Wonder my_git % git merge release

更新 755249b..7c1b37f

Fast-forward

hello.c | 1 +

1 file changed, 1 insertion(+)

——————————————————————————————————

17、假设生产中发现紧急问题则从对应的 tag版本中 checout一个 hotfix/分支

(base) never@Wonder my_git % git checkout -b hotfix/hello

切换到一个新分支 ‘hotfix/hello'

18、修改hello.c

(base) never@Wonder my_git % vi hello.c

(base) never@Wonder my_git % git add hello.c

(base) never@Wonder my_git % git commit hello.c

[hotfix/hello 38a07fb] hotfix , data overflow

1 file changed, 1 insertion(+)

——————————————————————————————————

19、hello.c合并到 dev

(base) never@Wonder my_git % git checkout dev

D a.txt

D b.txt

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

切换到分支 'dev'

您的分支领先 'origin/dev' 共 2 个提交。

  (使用 "git push" 来发布您的本地提交)

(base) never@Wonder my_git % git merge hotfix/hello

更新 7c1b37f..38a07fb

Fast-forward

hello.c | 1 +

1 file changed, 1 insertion(+)

——————————————————————————————————

20、hello.c合并到 master

(base) never@Wonder my_git % git checkout master

D a.txt

D b.txt

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

切换到分支 'master'

您的分支领先 'origin/master' 共 2 个提交。

  (使用 "git push" 来发布您的本地提交)

(base) never@Wonder my_git % git merge hotfix/hello

更新 7c1b37f..38a07fb

Fast-forward

hello.c | 1 +

1 file changed, 1 insertion(+)

————————————   end   ——————————————————

(三)Git 其他补充操作

1、查看日志

(1)查看日志(全部信息)

(base) never@Wonder my_git % git log

commit 462eb9404f79540eabaea5740a373c985a5ebc10 (HEAD -> release, origin/release)

Merge: 7c1b37f cc452f5

Author: wangbao <1073559082@qq.com>

Date:   Wed Nov 30 17:48:28 2022 +0800

    now Merge branch 'fix/hello' into release

(2)查看日志(缩写为一行)

(base) never@Wonder my_git % git log --pretty=oneline

462eb9404f79540eabaea5740a373c985a5ebc10 (HEAD -> release, origin/release) now Merge branch 'fix/hello' into release

(3)查看日志(缩写首部编码)

(base) never@Wonder my_git % git log --abbrev-commit

commit 462eb94 (HEAD -> release, origin/release)

Merge: 7c1b37f cc452f5

Author: wangbao <1073559082@qq.com>

Date:   Wed Nov 30 17:48:28 2022 +0800

    now Merge branch 'fix/hello' into release

(4)查看日志(图形化显示)

(base) never@Wonder my_git % git log --graph      

*   commit 462eb9404f79540eabaea5740a373c985a5ebc10 (HEAD -> release, origin/release)

|\  Merge: 7c1b37f cc452f5

| | Author: wangbao <1073559082@qq.com>

| | Date:   Wed Nov 30 17:48:28 2022 +0800

| |

| |     now Merge branch 'fix/hello' into release

| |

| * commit cc452f54db1e81dcdf673675a149618402c7d8d2 (origin/fix/hello, fix/hello)

| | Author: wangbao <1073559082@qq.com>

| | Date:   Wed Nov 30 17:47:46 2022 +0800

| |

| |     hello

| |

* | commit 7c1b37fef801de232d3929ae707ea20557889b36 (tag: v1.0-release)

|/  Author: wangbao <1073559082@qq.com>

|   Date:   Wed Nov 30 17:17:00 2022 +0800

|   

|       problem is fixed successful

|

(5)查看日志(图形化显示)

(base) never@Wonder my_git % git log --graph --pretty=onelin --abbrev-commit

*   462eb94 (HEAD -> release, origin/release) now Merge branch 'fix/hello' into release

|\

| * cc452f5 (origin/fix/hello, fix/hello) hello

* | 7c1b37f (tag: v1.0-release) problem is fixed successful

|/

* 755249b (origin/feature/bookshop, feature/bookshop) test git commit

2、比较两个指定的分支

(base) never@Wonder my_git % git diff  master fix/hello

diff --git a/hello.c b/hello.c

index 4f4a6ef..3ea411a 100644

--- a/hello.c

+++ b/hello.c

@@ -1,3 +1,2 @@

+now

this is hello.c

-here occured a problem , but i have fixed it

-this is hotfix/hello , i have fixed a urgent problem

3、退回 git add 到暂存区的内容

(1)对指定文件的取消 add 操作

(base) never@Wonder my_git % touch abc.txt

(base) never@Wonder my_git % git add abc.txt

(base) never@Wonder my_git % git reset HEAD abc.txt

重置后取消暂存的变更:

D a.txt

D b.txt

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

(2)取消最近一次的 add 操作

(base) never@Wonder my_git % touch aaa.txt         

(base) never@Wonder my_git % git add aaa.txt

(base) never@Wonder my_git % git reset HEAD      

重置后取消暂存的变更:

D a.txt

D b.txt

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

4、commit 内容回退

(1)回退最近一次 commit 操作

(base) never@Wonder my_git % git commit --amend

[release fe7ce1f] sorry,now Merge branch 'fix/hello' into release

Date: Wed Nov 30 17:48:28 2022 +0800

(2)回退指定 commit-id 的commit操作

@1、git reset --soft // 回退到指定commit,该commit之后的提交内容,保留工作目录,并把重置 HEAD 所带来的新的差异放进暂存区

(base) never@Wonder my_git % git reset --soft fe7ce1f

@2、git reset --hard // 回退到指定commit,该commit之后的提交内容,工作区和暂存区的内容都被抹掉

(base) never@Wonder my_git % git reset --hard c55d1b3

HEAD 现在位于 c55d1b3 my_new is a text

@3、git reset 或 git reset --mixed // 不带参数,或带参数–mixed(默认参数),与git reset --soft 不同,它将会把差异放到工作区

(base) never@Wonder my_git % git reset 755249b

重置后取消暂存的变更:

D a.txt

D b.txt

M hello.c

D m.txt

D my_branch.txt

D my_file.txt

D my_new.txt

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值