Git:版本回退

上一篇文章中,我们使用了README.MD文件作为演示:

## README

* This a readme file.
* This file is used to study git.

> Git is a free version control system.

下面,我们重复修改几次:

  • 版本2:
## README

* This a readme file by Joshuaber.
* This file is used to study git.

> Git is a free distributed version control system.
  • 版本3:
## README

* This a readme file by Joshuaber.
* This file is used to study git.
* Time: 2016-12-05

> Git is a free distributed version control system.

查看日志

git log命令能够让我们看到什么时间做了什么改变:

~ git log
commit d3c2d67600ecfbc283262779bc0253ce939be05f
Author: Joshuaber <[email protected]>
Date:   Mon Dec 5 11:32:47 2016 +0800

    add time

commit efc11c25098c65b8d927e4f1946588c48f0195d9
Author: Joshuaber <[email protected]>
Date:   Mon Dec 5 11:30:52 2016 +0800

    add author

commit e94861f73c80adf48521e480d4f509ba92be5a4a
Author: Joshuaber <[email protected]>
Date:   Mon Dec 5 10:37:45 2016 +0800

    wrote a readme file
(END)

如果信息太多,我们可以使用--pretty=oneline参数:

~ git log --pretty=oneline
d3c2d67600ecfbc283262779bc0253ce939be05f add time
efc11c25098c65b8d927e4f1946588c48f0195d9 add author
e94861f73c80adf48521e480d4f509ba92be5a4a wrote a readme file
(END)

版本回退

下面我们将使用git reset回退到上一个版本:

~ git reset --hard HEAD^
HEAD is now at efc11c2 add author

可以看到文件中的内容为:

## README

* This a readme file by Joshuaber.
* This file is used to study git.

> Git is a free distributed version control system.

此时再用git log命令查看日志:

~ git log
commit efc11c25098c65b8d927e4f1946588c48f0195d9
Author: Joshuaber <[email protected]>
Date:   Mon Dec 5 11:30:52 2016 +0800

    add author

commit e94861f73c80adf48521e480d4f509ba92be5a4a
Author: Joshuaber <[email protected]>
Date:   Mon Dec 5 10:37:45 2016 +0800

    wrote a readme file
(END)

add time那条记录已经不在了

回退几个版本就可以用几个^

当然我们也可以使用commit id指定回退到哪个版本(ID不需要写完全):

~ git reset --hard efc11
HEAD is now at efc11c2 add author

当然,如果我们回退之后后悔了想恢复怎么办。Certainly, 我们可以这样:

~ git reflog
efc11c2 HEAD@{0}: reset: moving to HEAD^
d3c2d67 HEAD@{1}: commit: add time
efc11c2 HEAD@{2}: commit: add author
e94861f HEAD@{3}: commit (initial): wrote a readme file
~
~ git reset --hard d3c2d67
HEAD is now at d3c2d67 add time

总结

  • HEAD指向的版本就是当前版本,Git允许我们在版本之间切换,使用命令git reset --hard commit_id
  • git log可以查看提交历史,以便确定要回退到哪个版本。
  • git reflog查看命令历史,以便确定要回到哪个版本。
我就看着你吹牛

你可能感兴趣的:(Git:版本回退)