Git仓库-版本回滚

回滚到之前的版本

创建新文件“build.txt”,并提交三个版本

  • build 1

    1
    1st build.
  • build 2

    1
    2
    1st build.
    2nd build.
  • build 3

    1
    2
    3
    1st build.
    2nd build.
    3rd build.

查看log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ git log
commit 027619d39bbf66daf98f4911e804908e36da3a11
Author: yongfei.hu <huyongfei@huyongfei-pc.beyondsoft.com>
Date: Tue Nov 10 11:18:59 2015 +0800

3rd build

commit a5a34782dbef50569a18ead65014083c95297de7
Author: yongfei.hu <huyongfei@huyongfei-pc.beyondsoft.com>
Date: Tue Nov 10 11:18:33 2015 +0800

2nd build

commit bed53badabb724a6b8eda2d12de6287df0e065f2
Author: yongfei.hu <huyongfei@huyongfei-pc.beyondsoft.com>
Date: Tue Nov 10 11:18:14 2015 +0800

1st build

回退到第2个版本(2nd build)

1
2
$ git reset --hard HEAD^
HEAD is now at a5a3478 2nd build

查看工作区文档,已经是回退到‘2nd build’。

Note: 在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,往上100个版本写成HEAD~100。

回滚后,恢复到最新版本

  • 如果当前bash窗口没有关闭,找到最新版本的‘commit id’,再指定到上面即可

    1
    2
    $ git reset --hard 027619d
    HEAD is now at 027619d 3rd build

  • 如果当前bash窗口已关闭,git reflog用来记录你的每一次命令

    1
    2
    3
    4
    5
    $ git reflog
    a5a3478 HEAD@{0}: reset: moving to HEAD^
    027619d HEAD@{1}: commit: 3rd build
    a5a3478 HEAD@{2}: commit: 2nd build
    bed53ba HEAD@{3}: commit: 1st build

再指定到相应的commit id上就好。

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!