git学习02 - log查看&版本回退

时间:2022-04-05 20:17:00

1、查看更新记录 git log /  git log --pretty=oneline

D:\learn\git_test>git log
commit a915e7b12076673d778da2abad8d7b11b681d88d (HEAD -> master)
Author: xiong <jasminexiong@yunify.com>
Date:   Tue Apr  ::  +

    add a distributed word

commit eaf3eea813568389f0213eb56f1ac8456b0c6fb8
Author: xiong <jasminexiong@yunify.com>
Date:   Tue Apr  ::  +

    wrote a readme file
############################################
D:\learn\git_test>git log --pretty=oneline
a915e7b12076673d778da2abad8d7b11b681d88d (HEAD -> master) add a distributed word
eaf3eea813568389f0213eb56f1ac8456b0c6fb8 wrote a readme file

  其中,前面的一串很长的数字为sha1算法计算得来的commit id,是每一个版本的唯一标识,若需要回退时则需要使用

  HEAD 代表为当前版本

2、回退版本 git reset --hard commit-id

D:\learn\git_test>git log --pretty=oneline
a915e7b12076673d778da2abad8d7b11b681d88d (HEAD -> master) add a distributed word
eaf3eea813568389f0213eb56f1ac8456b0c6fb8 wrote a readme file

D:\learn\git_test>git reset --hard eaf3
HEAD is now at eaf3eea wrote a readme file

D:\learn\git_test>git log --pretty=oneline
eaf3eea813568389f0213eb56f1ac8456b0c6fb8 (HEAD -> master) wrote a readme file

D:\learn\git_test>type readme.txt
Git is a version control system
Git is a free software

  commit-id可以简写,git可自动找到该版本

若由较新的版本更新为旧版本,则再使用git log时看不到新版本的信息

3、若想再更新到原来的新版本 通过 git reflog 找到原版本的commit-id,再执行reset即可

D:\learn\git_test>git reflog
eaf3eea (HEAD -> master) HEAD@{}: reset: moving to eaf3
a915e7b HEAD@{}: reset: moving to HEAD
a915e7b HEAD@{}: commit: add a distributed word
eaf3eea (HEAD -> master) HEAD@{}: commit (initial): wrote a readme file

D:\learn\git_test>git reset --hard a915e7b
HEAD is now at a915e7b add a distributed word

D:\learn\git_test>type readme.txt
Git is a distributed version control system
Git is a free software