git-版本回退

继续修改readme.txt文件:

git is a distributed version control system.
git is free software distributed under the GPL.

然后提交:

 zhangkai@bogon  ~/git_zk/learngit   master  vim readme.txt 
 zhangkai@bogon  ~/git_zk/learngit   master ●  git add readme.txt 
 zhangkai@bogon  ~/git_zk/learngit   master ✚  git commit -m "add GPL"
[master b2ba41f] add GPL
 1 file changed, 1 insertion(+), 1 deletion(-)
 zhangkai@bogon  ~/git_zk/learngit   master  

然后看一下我们的历史记录git log:

commit b2ba41f1f8758e77da8b08c685a587227de36fa9
Author: zhangkai <zhangkai910606@163.com>
Date:   Fri Apr 22 09:59:40 2016 +0800

    add GPL

commit 9fc42f13cd4db1231025049a6f0218aa9176db7b
Author: zhangkai <zhangkai910606@163.com>
Date:   Fri Apr 22 09:49:51 2016 +0800

    add distributed

commit 719d8537c31f45b9f3c9700acf9e8871c59fa638
Author: zhangkai <zhangkai910606@163.com>
Date:   Thu Apr 21 17:50:10 2016 +0800

    add abc

commit 80b51be3c77262a9e84a7ef5f083651d4d80a738
Author: zhangkai <zhangkai910606@163.com>
Date:   Thu Apr 21 10:07:05 2016 +0800

    add 3 file

commit b35460662bf2948a77ee2f12b2e9371d8b320bcb
Author: zhangkai <zhangkai910606@163.com>
Date:   Thu Apr 21 09:58:26 2016 +0800

    add readme file
(END)

可以看到最近一次提交时add GPL,可以看到每次的提交

用git log显示的日志太多了加一个参数--pretty=oneline可以看到简明的日志:

b2ba41f1f8758e77da8b08c685a587227de36fa9 add GPL
9fc42f13cd4db1231025049a6f0218aa9176db7b add distributed
719d8537c31f45b9f3c9700acf9e8871c59fa638 add abc
80b51be3c77262a9e84a7ef5f083651d4d80a738 add 3 file
b35460662bf2948a77ee2f12b2e9371d8b320bcb add readme file
(END)

从上面看到其中b2ba...值commit id也就是版本号是唯一的。

在git中用HEAD表示当前的版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,如果上100个版本写成HEAD~100。

目前当前的版本是add GPL,现在要回退到add distributed,用git reset命令:

zhangkai@bogon  ~/git_zk/learngit   master  git reset --hard HEAD^
HEAD is now at 9fc42f1 add distributed

然后看一下内容和历史状态:

zhangkai@bogon  ~/git_zk/learngit   master  cat readme.txt 
git is a distributed version control system.
git is free software.
zhangkai@bogon  ~/git_zk/learngit   git log --pretty=oneline 
9fc42f13cd4db1231025049a6f0218aa9176db7b add distributed
719d8537c31f45b9f3c9700acf9e8871c59fa638 add abc
80b51be3c77262a9e84a7ef5f083651d4d80a738 add 3 file
b35460662bf2948a77ee2f12b2e9371d8b320bcb add readme file
(END)

现在版本已经回退到了上一个版本了,但是通过git log已经看不到现在最新的那个版本了,如果刚刚你的那个窗口没有关掉,可以通过add GPL的commit id号重新再回来:

 zhangkai@bogon  ~/git_zk/learngit   master  git log --pretty=oneline 
 zhangkai@bogon  ~/git_zk/learngit   master  git reset --hard b2ba41f
HEAD is now at b2ba41f add GPL
 zhangkai@bogon  ~/git_zk/learngit   master  cat readme.txt 
git is a distributed version control system.
git is free software distributed under the GPL.
 zhangkai@bogon  ~/git_zk/learngit   master  

ok现在就回来了.

当git reset --hard HEAD^回退到add distributed版本时,在想恢复到add GPL就必须找到它的commit id,git中git reflog用来记录操作的每条命令:

 zhangkai@bogon  ~/git_zk/learngit   master  git reflog 
b2ba41f HEAD@{0}: reset: moving to b2ba41f
9fc42f1 HEAD@{1}: reset: moving to HEAD^
b2ba41f HEAD@{2}: commit: add GPL
9fc42f1 HEAD@{3}: commit: add distributed
719d853 HEAD@{4}: commit: add abc
80b51be HEAD@{5}: commit: add 3 file
b354606 HEAD@{6}: commit (initial): add readme file

从上面就可以看到add GPL的commit id了,然后通过git reset就可以回来了。

小结

  HEAD指向的版本就是当前的版本,因此git可以在版本的历史中穿梭使用命令:git reset --hard commit_id.

  用git log可以查看提交历史,以便确定要回退到哪个版本。

  如果想要回到最新的版本中,用git reflog查看历史命令,以便确定要回到未来的哪个版本。

posted @ 2016-04-22 10:36  汪汪小喵咪  阅读(180)  评论(0编辑  收藏  举报