zoukankan      html  css  js  c++  java
  • Git 版本回退

    我们再修改一下readme.txt文件如下:

    Git is a distributed version control system
    Git is free software under the GPL.

    然后尝试提交:

    $ git add readme.txt

    $ git commit -m "add GPL"
    [master 8975acf] add GPL
    1 file changed, 1 insertion(+), 1 deletion(-)

    如果误删了什么内容提交了,怎么样可以回退到最近的一个版本重新开始呢?

      我们一共有三个版本被提交到了Git仓库里了,这是我们人为记得,在实际工作中,我们的脑子怎么记得一个几千行的文件每次都改了什么东西了。

    版本控制系统肯定有个命令告诉我们的历史记录,在Git中,我们利用git log命令查看:

    $ git log

    commit 8975acf33555932316172fb9edd73844a08d478f
    Author: LV <lvyonggang04260715@163.com>
    Date: Sat May 14 14:12:29 2016 +0800

    add GPL

    commit 4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7
    Author: LV <lvyonggang04260715@163.com>
    Date: Sat May 14 13:55:44 2016 +0800

    modify the readme.txt commit

    commit 2141c92f28b51cbe35008c8d66b8d0e31f9257c1
    Author: LV <lvyonggang04260715@163.com>
    Date: Sat May 14 12:56:36 2016 +0800

    wrote readme file

    git log命令显示从最近到最远的提交日志,我们可以看到3次提交,如果嫌输出的信息过多,可以加上参数 --pretty=oneline参数:

    $ git log --pretty=oneline
    8975acf33555932316172fb9edd73844a08d478f add GPL
    4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7 modify the readme.txt commit
    2141c92f28b51cbe35008c8d66b8d0e31f9257c1 wrote readme file

    "8975acf33555932316172fb9edd73844a08d478f"这段字符串是commit id(版本号)

    每提交一个新的版本,实际上Git就会把它们自动串成一条时间线,如果使用可视化化工具查看Git历史,就可以更清楚的看到提交历史的时间线

    现在我们准备把readme.txt回退到上一个版本:

    首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交"8975acf33555932316172fb9edd73844a08d478f",

    上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^容易自己数不过来,多以就写成HEAD~100.

    现在,我们要把当前的版本"add GPL"回退到上一个版本"modify the readme.txt commit",就可以使用git reset命令:

    $ git reset --hard HEAD^
    HEAD is now at 4b1d71b modify the readme.txt commit

    --hard参数有啥意义?这个在后面再讲,现在先放心使用。

    看看readme.txt内容是不是版本modify the readme.txt commit

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

    果然回退到了"modify the readme.txt commit"的这个版本,我们还可以继续回退,不过且慢,我们用git log再看一下现在版本的状态:

    $ git log
    commit 4b1d71bafb2cc7bec44cff1c7e3e305ea01888d7
    Author: LV <lvyonggang04260715@163.com>
    Date: Sat May 14 13:55:44 2016 +0800

    modify the readme.txt commit

    commit 2141c92f28b51cbe35008c8d66b8d0e31f9257c1
    Author: LV <lvyonggang04260715@163.com>
    Date: Sat May 14 12:56:36 2016 +0800

    wrote readme file

    最新的那个"add GPL"版本已经看不到了,如果我们在回退了之后再想回去怎么办?只要当前的命令行窗口没有关的话,我们就去找,找到"add GPL"的commit id就能返回去

    $ git reset --hard 8975acf335559 //commit id可以不用写全,但是也不能只写前几位,因为Git可能找到多个版本,无法确定是哪一个,位数越多越好,最好是全部
    HEAD is now at 8975acf add GPL

    查看当前readme.txt内容:

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

    神奇的回退了有没有?

    Git的版本回退速度非常快的,因为Git在内部有个指向当前版本的HEAD指针,当你回退版本的时候,Git仅仅是把HEAD从指向"add GPL"

    当你回退到某个版本,想要回退到新版本,又找不到新版本的commit id怎么办?Git提供了一个命令 git reflog用来记录你的每一次命令:

    8975acf HEAD@{0}: reset: moving to 8975acf335559
    4b1d71b HEAD@{1}: reset: moving to HEAD^
    8975acf HEAD@{2}: commit: add GPL
    4b1d71b HEAD@{3}: commit: modify the readme.txt commit
    2141c92 HEAD@{4}: commit (initial): wrote readme file

  • 相关阅读:
    Rancher中删除k8s节点数据,还原机器用于重新安装
    k8s、pod中的应用获取宿主机IP、PODIP等信息
    通过物理设备了解Osi网络架构
    el-tree懒加载无子级数据时去掉下拉箭头
    使用axios.all和axios.spread处理并发请求
    element日期控件修改时候显示1970年的问题
    echarts柱状图区域缩放可拖动参数配置
    记录element表格设置右侧固定,边框线消失的问题
    echarts饼图数据为0时隐藏数据指示线
    vue使用moment.js处理时间格式
  • 原文地址:https://www.cnblogs.com/LvLoveYuForever/p/5492656.html
Copyright © 2011-2022 走看看