Git教程-版本回退

Git教程-版本回退

    • 版本回退

版本回退

1.对readme.txt修改了三次分别提交,git log查看历史记录:

$ git log
commit 509c3909cd3867071d3943ad5d40b920087cf13f (HEAD -> master)
Author: lky 
Date:   Thu May 23 16:56:54 2019 +0800

    append GPL

commit 31e52208868f209c92b5ca7d8d27100a9b4e9220
Author: lky 
Date:   Thu May 23 16:27:33 2019 +0800

    add distributed

commit f19662ee401c96f4417d347c46090ff0ed70bd5a
Author: lky 
Date:   Thu May 23 15:52:08 2019 +0800

    wrote a readme file

  1. git log --pretty=oneline输出简要信息
$ git log --pretty=oneline
509c3909cd3867071d3943ad5d40b920087cf13f (HEAD -> master) append GPL
31e52208868f209c92b5ca7d8d27100a9b4e9220 add distributed
f19662ee401c96f4417d347c46090ff0ed70bd5a wrote a readme file

  1. 前面的数字是Git的commit id。
    当前的最新版本是append GPL,想要回到add distribute版本,使用$ git reset --hard HEAD^命令,其中HEAD是当前版本,HEAD^ 是上一个版本,HEAD^^是上上个版本,往上第100个版本是HEAD~100.
$ git reset --hard HEAD^
HEAD is now at 31e5220 add distributed

  1. 此时的readme.txt中已经是add distributed那一版。
    想要再恢复到append GPL这一版,可以用git reset --hard 509c3,其中509c3是commit id的前几位。当不知道这个id但是又想回复到新版本时,使用git reflog命令可以查看每一次的命令, 可以看到append GPL当前的id
$ git reflog
31e5220 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
509c390 HEAD@{1}: reset: moving to 509c3
31e5220 (HEAD -> master) HEAD@{2}: reset: moving to HEAD^
509c390 HEAD@{3}: commit: append GPL
31e5220 (HEAD -> master) HEAD@{4}: commit: add distributed
f19662e HEAD@{5}: commit (initial): wrote a readme file

你可能感兴趣的:(Git教程-版本回退)