GIT学习----第三节:版本回退

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38082783/article/details/82894544

学习目的

  1. git log: 查看版本信息;
  2. git log --pretty=oneline: 简化查看到的版本信息;
  3. git reset --hard HEAD^: 版本回退到上个版本;
  4. git reset --hard HEAD^^: 版本回退到上上个版本;
  5. git reset --hard HEAD~100: 版本回退到100个版本;
  6. git reset --hard “commit id”: 版本回退到具体的版本;
  7. cat : 查看当前版本的内容;
  8. git reflog: 查看"commit id".

git log命令查看版本用

$ git log
commit abef5fa90eda7769a67ea5647df048a3b790830d (HEAD -> master)
Author: Rattenking <[email protected]>
Date:   Fri Sep 28 17:04:51 2018 +0800

    my test git file.

commit e2addc717e8e35a61c1748219095b06fd9cbff46
Author: Rattenking <[email protected]>
Date:   Fri Sep 28 16:54:05 2018 +0800

    添加了两句话

commit 101462868fcc996bd42169709499dbd33975847c
Author: Rattenking <[email protected]>
Date:   Fri Sep 28 14:30:41 2018 +0800

    wrote a readme file

通过查看版本信息可以看出:

  1. 版本一:
wrote a readme file
  1. 版本二:
添加了两句话
  1. 版本三:
my test git file.

git log命令显示从最近到最远的提交日志。

简化返回信息命令git log --pretty=oneline

$ git log --pretty=oneline
abef5fa90eda7769a67ea5647df048a3b790830d (HEAD -> master) my test git file.
e2addc717e8e35a61c1748219095b06fd9cbff46 添加了两句话
101462868fcc996bd42169709499dbd33975847c wrote a readme file

需要友情提示的是,你看到的一大串类似abef5fa90eda7769a67ea5647df048a3b790830d的是commit id(版本号),和SVN不一样,Git的commit id不是1,2,3……递增的数字,而是一个SHA1计算出来的一个非常大的数字,用十六进制表示,而且你看到的commit id和我的肯定不一样,以你自己的为准。为什么commit id需要用这么一大串数字表示呢?因为Git是分布式的版本控制系统,后面我们还要研究多人在同一个版本库里工作,如果大家都用1,2,3……作为版本号,那肯定就冲突了。

版本回退命令git reset

准备把readme.txt回退到上一个版本,也就是“添加了两句话”的那个版本,怎么做呢?


首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交abef5fa90eda7769a67ea5647df048a3b790830d(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD,上上一个版本就是HEAD,当然往上100个版本写100个比较容易数不过来,所以写成HEAD~100。


现在,我们要把当前版本my test git file.回退到上一个版本“添加了两句话”,就可以使用git reset命令:

$ git reset --hard HEAD^
HEAD is now at e2addc7 添加了两句话

查看当前版本内容cat

$ cat readme.txt
Git is a version control system.
Git is free software.
Git is a distributed version control system.
Git is free software.

查看版本库状态git log:

$ git log
commit e2addc717e8e35a61c1748219095b06fd9cbff46 (HEAD -> master)
Author: Rattenking <[email protected]>
Date:   Fri Sep 28 16:54:05 2018 +0800

    添加了两句话

commit 101462868fcc996bd42169709499dbd33975847c
Author: Rattenking <[email protected]>
Date:   Fri Sep 28 14:30:41 2018 +0800

    wrote a readme file

会发现已经回到“添加了两句话”的版本状态。


在命令窗口没有关闭的情况,我们又想要“my test git file.”,怎么办?

  1. 找到“my test git file.”版本对应的commit id;
  2. $ git reset --hard “commit id”。
$ git reset --hard abef5fa90eda7769a67ea5647df048a3b790830d
HEAD is now at abef5fa my test git file.

再次查看版本库状态git log:会发现又回到了my test git file.版本

$ git log
commit abef5fa90eda7769a67ea5647df048a3b790830d (HEAD -> master)
Author: Rattenking <[email protected]>
Date:   Fri Sep 28 17:04:51 2018 +0800

    my test git file.

commit e2addc717e8e35a61c1748219095b06fd9cbff46
Author: Rattenking <[email protected]>
Date:   Fri Sep 28 16:54:05 2018 +0800

    添加了两句话

commit 101462868fcc996bd42169709499dbd33975847c
Author: Rattenking <[email protected]>
Date:   Fri Sep 28 14:30:41 2018 +0800

    wrote a readme file

关闭命令窗口查找commit id的命令git reflog

$ git reflog
abef5fa (HEAD -> master) HEAD@{0}: reset: moving to abef5fa90eda7769a67ea5647df048a3b790830d
e2addc7 HEAD@{1}: reset: moving to HEAD^
abef5fa (HEAD -> master) HEAD@{2}: commit: my test git file.
e2addc7 HEAD@{3}: commit: 添加了两句话
1014628 HEAD@{4}: commit (initial): wrote a readme file

从输出可知,“my test git file.”的commit id是abef5fa,现在,这时就可以通过commit id回退到你后悔的版本。

小结

  1. HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id。
  2. 用git log可以查看提交历史,以便确定要回退到哪个版本。
  3. 要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。
  4. 查看当前版本的具体内容:用cat命令查看。

其他

QQ交流群: 264303060

QQ交流群

我的博客,欢迎交流!

我的CSDN博客,欢迎交流!

微信小程序专栏

前端笔记专栏

微信小程序实现部分高德地图功能的DEMO下载

微信小程序实现MUI的部分效果的DEMO下载

微信小程序实现MUI的GIT项目地址

微信小程序实例列表

前端笔记列表

游戏列表

猜你喜欢

转载自blog.csdn.net/m0_38082783/article/details/82894544
今日推荐