回到顶部


历史记录

查看git的历史记录 git log命令

$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 20 15:11:49 2013 +0800
​
    append GPL
​
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 20 14:53:12 2013 +0800
​
    add distributed
​
commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 19 17:51:55 2013 +0800
​
    wrote a readme fil
​
ps:git log命令显示从最近到最远的提交日志,我们可以看到3次提交,最近的一次是append GPL,上一次是add distributed,最早的一次是wrote a readme file。
如果嫌输出的信息太多,可以在 git log 后加上"--pretty=oneline"
​
$ git log --pretty=oneline
  26d48395383f8f31179f24e0882e1e0 append GPL
ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file
​
ps: 一大串类似3628164...882e1e0的是commit id(版本号)

版本回退

首先git必须知道当前的版本是哪个版本
 在git中HEAD 表示当前版本,也就是最新提交的信息版本 ,上一个版本就是HEAD^ ,上上一个版本就是 HEAD^^,^太多是这样写 HEAD~100
​
当前版本会退到上一个版本
  git reset --hard HEAD^
返回最新版本
  git reset --hard  3628164fb(ps:版本号)
​
版本号没必要写全,前几位就可以,git会自动去找的。如果忘记版本号,使用命令 git reflog来查看每一次的命令来找到最新的版本号。
$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file
​```
​
###版本重新返回

当回退到某个版本后,想重新回到最新的版本。
当使用 $git reset --head HEAD^ 回退到最新版本的前一个版本应该如何返回,这时想返回到最新的版本,就必须要找到最新版本的commit id。
  git reflog
  git提供一个命令git reflog 用来记录每一次命令

$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file

### 回到最新版本(回到未来) 

$git reset --hard ea34578

###git版本使用总结

HEAD 指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令 git reset --hard commit_id
git log 可以查看提交的历史记录,以便于要回退到那个版本
git reflog 查看命令历史,以便确定要回到未来的那个版本

posted on 2018-04-19 09:17  ssgao  阅读(129)  评论(0)    收藏  举报