上一篇文章中堕扶,我們使用了README.MD文件作為演示:
## README
* This a readme file.
* This file is used to study git.
> Git is a free version control system.
下面友题,我們重復(fù)修改幾次:
- 版本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
命令能夠讓我們看到什么時(shí)間做了什么改變:
~ git log
commit d3c2d67600ecfbc283262779bc0253ce939be05f
Author: Joshuaber <1174980997@qq.com>
Date: Mon Dec 5 11:32:47 2016 +0800
add time
commit efc11c25098c65b8d927e4f1946588c48f0195d9
Author: Joshuaber <1174980997@qq.com>
Date: Mon Dec 5 11:30:52 2016 +0800
add author
commit e94861f73c80adf48521e480d4f509ba92be5a4a
Author: Joshuaber <1174980997@qq.com>
Date: Mon Dec 5 10:37:45 2016 +0800
wrote a readme file
(END)
如果信息太多形真,我們可以使用--pretty=oneline
參數(shù):
~ git log --pretty=oneline
d3c2d67600ecfbc283262779bc0253ce939be05f add time
efc11c25098c65b8d927e4f1946588c48f0195d9 add author
e94861f73c80adf48521e480d4f509ba92be5a4a wrote a readme file
(END)
版本回退
下面我們將使用git reset
回退到上一個(gè)版本:
~ git reset --hard HEAD^
HEAD is now at efc11c2 add author
可以看到文件中的內(nèi)容為:
## README
* This a readme file by Joshuaber.
* This file is used to study git.
> Git is a free distributed version control system.
此時(shí)再用git log
命令查看日志:
~ git log
commit efc11c25098c65b8d927e4f1946588c48f0195d9
Author: Joshuaber <1174980997@qq.com>
Date: Mon Dec 5 11:30:52 2016 +0800
add author
commit e94861f73c80adf48521e480d4f509ba92be5a4a
Author: Joshuaber <1174980997@qq.com>
Date: Mon Dec 5 10:37:45 2016 +0800
wrote a readme file
(END)
add time
那條記錄已經(jīng)不在了
回退幾個(gè)版本就可以用幾個(gè)^
當(dāng)然我們也可以使用commit id
指定回退到哪個(gè)版本(ID不需要寫(xiě)完全):
~ git reset --hard efc11
HEAD is now at efc11c2 add author
當(dāng)然,如果我們回退之后后悔了想恢復(fù)怎么辦。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
總結(jié)
- HEAD指向的版本就是當(dāng)前版本,Git允許我們?cè)诎姹局g切換欠肾,使用命令
git reset --hard commit_id
。 -
git log
可以查看提交歷史拟赊,以便確定要回退到哪個(gè)版本刺桃。 -
git reflog
查看命令歷史,以便確定要回到哪個(gè)版本吸祟。
我就看著你吹牛