• 6.Git代码回滚


    1.代码修改并提交

    我们已经成功地添加并提交了一个helloWorld.txt文件,现在,是时候继续工作了.
    于是,我们继续修改helloWorld.txt文件,改成如下内容:
        $ vi helloWorld.txt
        $ cat helloWorld.txt
        hello world !
        first:di yi ci xiugai!
    
    现在,运行`git status`命令看看结果:
        $ git status
        On branch dev
        Changes not staged for commit:
          (use "git add <file>..." to update what will be committed)
          (use "git checkout -- <file>..." to discard changes in working directory)
    
                modified:   helloWorld.txt
    
        no changes added to commit (use "git add" and/or "git commit -a")
    
    虽然Git告诉我们helloWorld.txt被修改了,但如果能看看具体修改了什么内容,自然是很好的.比如你休假两周从国外回来,第一天上班时,已经记不清上次怎么修改的helloWorld.txt.所以,需要用git diff这个命令看看:
        $ git diff
        warning: LF will be replaced by CRLF in helloWorld.txt.
        The file will have its original line endings in your working directory
        diff --git a/helloWorld.txt b/helloWorld.txt
        index a95200f..edc9ede 100644
        --- a/helloWorld.txt
        +++ b/helloWorld.txt
        @@ -1 +1,2 @@
         hello world !
        +first:di yi ci xiugai!
    输出中+号绿色显示的就是修改或新增的内容,-号红色显示的就是去掉或被修改的内容
    
    知道了对helloWorld.txt作了什么修改后,再把它提交到仓库就放心多了,提交修改和提交新文件是一样的两步
    
    第一步:执行命令`git add .`或`git add helloWorld.txt`
        $ git add helloWorld.txt
        warning: LF will be replaced by CRLF in helloWorld.txt.
        The file will have its original line endings in your working directory
    
    第二步:执行命令`git commit -m 'second'`
        $ git commit -m 'second'
        [dev 2a1df96] second
         1 file changed, 1 insertion(+)
    
    提交后,我们再用git status命令看看仓库的当前状态:
        $ git status
        On branch master
        Your branch is ahead of 'origin/master' by 1 commit.
          (use "git push" to publish your local commits)
        nothing to commit, working tree clean
        Git告诉我们当前没有需要提交的修改,而且,工作目录是干净(working directory clean)的.
    

    2.代码回滚

    现在,你已经学会了修改文件,然后把修改提交到Git版本库.现在,再练习一次,修改helloWorld.txt文件如下:
    $ vi helloWorld.txt 
    $ cat helloWorld.txt
    hello world !
    first:di yi ci xiugai!
    second:di er ci xiugai!
    
    用命令`git add`告诉Git,把文件添加到仓库
    $ git add .
    warning: LF will be replaced by CRLF in helloWorld.txt.
    The file will have its original line endings in your working directory
    
    用命令`git commit`告诉Git,把文件提交到仓库
    $ git commit -m 'third'
    [master ff66b4e] third
    1 file changed, 1 insertion(+)
    
    用命令`git status`查看状态
    $ git status
    On branch master
    Your branch is ahead of 'origin/master' by 1 commit.
     (use "git push" to publish your local commits)
    nothing to commit, working tree clean
    
    像这样,你不断对文件进行修改,然后不断提交修改到版本库里,就好比玩RPG游戏时,每通过一关就会自动把游戏状态存盘,如果某一关没过去,你还可 以选择读取前一关的状态.有些时候,在打Boss之前,你会手动存盘,以便万一打Boss失败了,可以从最近的地方重新开始.Git也是一样,每当你觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit.一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失.
    现在,我们回顾一下helloWorld.txt文件一共有几个版本被提交到Git仓库里了: 
    
    当然了,在实际工作中,我们脑子里怎么可能记得一个几千行的文件每次都改了什么内容,不然要版本控制系统干什么。版本控制系统肯定有某个命令可以告诉我们历史记录,在Git中,我们用git log命令查看:
    $ git log
    commit 36bc4daa8dbbd94487a5513d37cbfbfed14f1f2f (HEAD -> dev)
    Author: apollo1616 <1986690272@qq.com>
    Date:   Sat Jan 19 08:14:49 2019 +0800
        third
    
    commit 2a1df969e3a6845163be127f6ba36b093882bc77
    Author: apollo1616 <1986690272@qq.com>
    Date:   Sat Jan 19 08:12:12 2019 +0800
        second
    
    commit 44c0b650e2b0bcbd0671fc2252c66fb2eb83192c
    Author: apollo1616 <1986690272@qq.com>
    Date:   Sat Jan 19 08:09:04 2019 +0800
        first commit
    
    git log命令显示从最近到最远的提交日志,我们可以看到2次提交,最近的一次是second,最早的一次是first.
    如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数: 
    $ git log --pretty=oneline
    36bc4daa8dbbd94487a5513d37cbfbfed14f1f2f (HEAD -> dev) third
    2a1df969e3a6845163be127f6ba36b093882bc77 second
    44c0b650e2b0bcbd0671fc2252c66fb2eb83192c first commit
    
    需要友情提示的是,你看到的一大串类似ff66b4e...b5eb686的是commit id(版本号),和SVN不一样,Git的commit id不是1,2,3……递增的数字,而是一个SHA1计算出来的一个非常大的数字,用十六进制表示,而且你看到的commit id和我的肯定不一样,以你自己的为准.为什么commit id需要用这么一大串数字表示呢?因为Git是分布式的版本控制系统,后面我们还要研究多人在同一个版本库里工作,如果大家都用1,2,3……作为版本号,那肯定就冲突了.
    

    3.回滚回滚回滚

    好了,现在我们启动时光穿梭机,准备把helloWorld.txt回退到上一个版本,也就是“second”的那个版本,怎么做呢?
    首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交36bc4daa8dbbd94487a5513d37cbfbfed14f1f2f(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100.
    现在,我们要把当前版本“third”回退到上一个版本“second”,就可以使用git reset命令:
    
    Administrator@wanghua MINGW64 ~/Desktop/test (master)
    $ git reset --hard HEAD^
    HEAD is now at 2a1df96 second
    
    此时再看你的文件内容,果然就退回去了
    Administrator@wanghua MINGW64 ~/Desktop/test (master)
    $ cat helloWorld.txt
    hello world !
    first:di yi ci xiugai!
    
    继续再往前回退一个版本,不过且慢,然我们用git log再看看现在版本库的状态:
    $ git log --pretty=oneline
    2a1df969e3a6845163be127f6ba36b093882bc77 (HEAD -> dev) second
    44c0b650e2b0bcbd0671fc2252c66fb2eb83192c first commit
    
    最新的那个版本`third`已经看不到了!好比你从21世纪坐时光穿梭机来到了19世纪,想再回去已经回不去了,肿么办?
    办法其实还是有的,只要上面的命令行窗口还没有被关掉,你就可以顺着往上找啊找啊,找到那个third的commit id是36bc4daa8dbbd94487a5513d37cbfbfed14f1f2f,于是就可以指定回到未来的某个版本:
    
        $ git reset --hard 36bc4da
        HEAD is now at 36bc4da third
        
    版本号没必要写全,前几位就可以了,Git会自动去找.当然也不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个了.
    
    再小心翼翼地看看helloWorld.txt的内容:
    $ cat helloWorld.txt
    hello world !
    first:di yi ci xiugai!
    second:di er ci xiugai!
    
    果然,我胡汉三又回来了.
    Git的版本回退速度非常快.因为Git在内部有个指向当前版本的HEAD指针.当你回退版本的时候.Git仅仅是把HEAD从指向third
    
    现在,你回退到了某个版本,关掉了电脑,第二天早上就后悔了,想恢复到新版本怎么办?找不到新版本的commit id怎么办?
    在Git中,总是有后悔药可以吃的.当你用$ git reset --hard HEAD^回退到`second`版本时,再想恢复到最新`third`的版本,就必须找到`third`的commit id.Git提供了一个命令git reflog用来记录你的每一次命令:
    
        $ git reflog
        2a1df96 (HEAD -> dev) HEAD@{0}: reset: moving to HEAD^
        36bc4da HEAD@{1}: reset: moving to 36bc4da
        2a1df96 (HEAD -> dev) HEAD@{2}: reset: moving to HEAD^
        36bc4da HEAD@{3}: commit: third
        2a1df96 (HEAD -> dev) HEAD@{4}: commit: second
        44c0b65 HEAD@{5}: commit: first commit
    
    终于舒了口气,第四行显示`third`的commit id是36bc4da,现在,你又可以乘坐时光机回到未来了。 
    
  • 相关阅读:
    代理模式和策略模式的区别
    代理模式vs适配器模式vs外观模式 转
    装饰模式与代理模式的区别(转载)
    用Delphi实现动态代理(2):设计说明  转
    Java静态内部类的介绍
    非常好的Java反射例子
    Java多线程中的锁机制
    Java多线程共享数据、同步、通信
    Java程序运行超时后退出解决
    Java安全:运用加密技术保护Java源代码
  • 原文地址:https://www.cnblogs.com/apollo1616/p/10440055.html
Copyright © 2020-2023  润新知