插播个号外

mybatis分析系列文章(偏源码)

springboot系列文章(偏源码)

mybatis面试系列文章

以上文章都是纯原创,非转发,非抄袭!!!

1:背景介绍

我司有很多款产品,多款产品使用的是通过我们的一套系统来支持的,最近一个新产品上线因为数据过多出现了非常严重的性能问题,为了紧急解决这个问题,将系统调整为仅支持目前这一款产品的方式,稳定后因为其他产品也要上线,这时候就有问题了,代码已经不支持了,所以需要紧急恢复到之前的版本(这里说明下,该款产品不会引起性能问题),但是由于当时事发突然,没有来得及打分制,也就有了这篇文章的分享。

2:讲解方式

本文将会从无仓库,到最后恢复到历史版本一步一步的来进行,尽量让不同层次的读者都能有所收获。

3:正式开始

3.1: 在gitee创建仓库

在gitee创建测试仓库test-git-reset-to-old-version

3.2: 在本地创建仓库test-git-reset-to-old-version

3.2.1:创建test-git-reset-to-old-version文件夹

3.2.2:进入test-git-reset-to-old-version文件夹初始化仓库
$ pwd
/e/test/test-git-reset-to-old-version

cj@cj-PC MINGW64 /e/test/test-git-reset-to-old-version
$ git init
Initialized empty Git repository in E:/test/test-git-reset-to-old-version/.git/
3.2.3:创建文件11.txt并提交到本地
$ touch 11.txt

cj@cj-PC MINGW64 /e/test/test-git-reset-to-old-version (master)
$ vim 11.txt

cj@cj-PC MINGW64 /e/test/test-git-reset-to-old-version (master)
$ cat 11.txt
111

$ git add 11.txt
warning: LF will be replaced by CRLF in 11.txt.
The file will have its original line endings in your working directory.
cj@cj-PC MINGW64 /e/test/test-git-reset-to-old-version (master)

$ git commit -m '11 txt first commit'
[master (root-commit) 16cfe39] 11 txt first commit
warning: LF will be replaced by CRLF in 11.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 11.txt
3.2.4:远程仓库建立管理

添加远程仓库格式为git remote add <你给远程仓库起的名字,一般指定为origin> 远程仓库地址,git remote -v该命令为查看远程仓库信息。

$ git remote add origin https://gitee.com/dongsir2020/test-git-reset-to-old-version.git

cj@cj-PC MINGW64 /e/test/test-git-reset-to-old-version (master)
$ git remote -v
origin  https://gitee.com/dongsir2020/test-git-reset-to-old-version.git (fetch)
origin  https://gitee.com/dongsir2020/test-git-reset-to-old-version.git (push)
3.2.5:推送到远程
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 216 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/dongsir2020/test-git-reset-to-old-version.git
 * [new branch]      master -> master

此时远程仓库变为:
在这里插入图片描述

3.3: 再次修改并提交

3.3.1:查看当前版本

git log --pretty=oneline用于通过显示一行的方式来查看git 日志。

$ git log --pretty=oneline
16cfe39c0b3f4898f1c01bb9a2e593aac1feb548 11 txt first commit
3.3.2:再次并提交推送到远程

内容修改为222

$ cat 11.txt
222

$ git commit -m '111 txt second commit'
[master warning: LF will be replaced by CRLF in 11.txt.
The file will have its original line endings in your working directory.
22f8bc6] 111 txt second commit
warning: LF will be replaced by CRLF in 11.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 245 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/dongsir2020/test-git-reset-to-old-version.git
   16cfe39..22f8bc6  master -> master
3.3.3:再次查看日志
$ git log --pretty=oneline
22f8bc66fcfa47eee2d6b1f1d5b026c0c00e58ab 111 txt second commit
16cfe39c0b3f4898f1c01bb9a2e593aac1feb548 11 txt first commit
3.3.4:恢复到第一个版本

恢复到版本16cfe39c0b3f4898f1c01bb9a2e593aac1feb548该信息可以在git log中获取,实际操作就是你要恢复到的那次提交ID的值,使用的名称为git reset --hard <commit ID>

$ git reset --hard 16cfe39c0b3f4898f1c01bb9a2e593aac1feb548
HEAD is now at 16cfe39 11 txt first commit

需要注意的是因为我们已经推送了远程,在禁用-f的情况无法强制推送,这时候的解决方案是,在该历史版本基础上直接建立分支,然后推送到远程仓库即可。

3.4: 使用恢复的历史版本开发

3.4.1:打出新分支

命令为git checkout -b <new branch name>,该命令同时完成了打新分支+切换分支的工作=git branch + git checkout。命令git branch -a用于查看所有的分支即本地分支+远程分支

$ git checkout -b the-old-version
Switched to a new branch 'the-old-version'

  $ git branch -a
  master
* the-old-version
  remotes/origin/master

可以看到当前的本地分支有master,the-old-version,远程分支有remotes/origin/master,其中本地分支the-old-version为当前分支。

3.4.2:将新分支推送到远程

命令为git push <远程仓库名称> <要推送的本地分支名称>:<远程仓库名称(自己随意起)>,一般本地和远程名称保持一致,但是本例中为自定义的,建议工作中使用保持一致的方式。

$ git push origin the-old-version:the-old-version-remote
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
remote: Create a pull request for 'the-old-version-remote' on Gitee by visiting
remote:     https://gitee.com/dongsir2020/test-git-reset-to-old-version/pull/new/dongsir2020:the-old-version-remote...dongsir2020:master
To https://gitee.com/dongsir2020/test-git-reset-to-old-version.git
 * [new branch]      the-old-version -> the-old-version-remote

再次使用命令git branch -a查看所有分支:

$ git branch -a
  master
* the-old-version
  remotes/origin/master
  remotes/origin/the-old-version-remote

可以看到远程分支中已经增加了分支remotes/origin/the-old-version-remote,直接到gitee查看如下图:
在这里插入图片描述

5:都让开,我要喝瑞幸

在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐