四、版本回退

git log命令显示从最近到最远的提交日志,我们可以看到3次提交

$ git log

commit 3628164fb26d48395383f8f31179f24e0882e1e0

Author: Michael LiaoDate:  Tue Aug 20 15:11:49 2013 +0800

append GPL

commit ea34578d5496d7dd233c827ed32a8cd576c5ee85

Author: Michael LiaoDate:  Tue Aug 20 14:53:12 2013 +0800

add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030

Author: Michael LiaoDate:  Mon Aug 19 17:51:55 2013 +0800

wrote a readme file

如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:

$git log --pretty=oneline3628164fb26d48395383f8f31179f24e0882e1e0appendGPLea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed

cb926e7ea50ad11b8f9e909c05226233bf755030wrote a readme file

在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的 肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

$ git reset--hard HEAD^

HEADisnowatea34578adddistributed

git log

$ git log

commit ea34578d5496d7dd233c827ed32a8cd576c5ee85

Author: Michael LiaoDate:  Tue Aug 20 14:53:12 2013 +0800

add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030

Author: Michael LiaoDate:  Mon Aug 19 17:51:55 2013 +0800

wrote a readme file

最新的那个版本append GPL已经看不到了

可以顺着往上找啊找啊,找到那个append GPL的commit id是3628164...,于是就可以指定回到未来的某个版本:

$ git reset--hard3628164  //版本号没必要写全,前几位就可以了,Git会自动去找HEADisnowat3628164append GPL

现在,你回退到了某个版本,关掉了电脑,第二天早上就后悔了,想恢复到新版本怎么办?找不到新版本的commit id怎么办?

当你用$ git reset --hard HEAD^回退到add distributed版本时,再想恢复到append GPL,就必须找到append GPL的commit id。Git提供了一个命令git reflog用来记录你的每一次命令:

$ git reflog 

ea34578HEAD@{0}:reset:movingtoHEAD^

3628164HEAD@{1}:commit:appendGPL

ea34578HEAD@{2}:commit:adddistributed

cb926e7HEAD@{3}:commit(initial):wroteareadmefile

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