git版本回退

回退到上一个版本

git reset --hard HEAD^
git reset --hard HEAD~1

回退到某一个版本

git reflog
git reset --hard commit_id

 

准备环境:

$ git init

$ echo '1 commit' > test.txt
$ git add .
$ git commit -m '1 commit'

$ echo '2 commit' > test.txt
$ git add .
$ git commit -m '2 commit'


$ echo '3 commit' > test.txt
$ git add .
$ git commit -m '3 commit'

$ echo '4 commit' > test.txt
$ git add .
$ git commit -m '4 commit'

$ git log --oneline --all --graph --decorate
* 1efcf18 (HEAD -> master) 4 commit
* 6a7ace8 3 commit
* 3be5879 2 commit
* 3d1a4d4 1 commit

 

回退一个版本

$ git reset --hard HEAD^
HEAD is now at 6a7ace8 3 commit

$ cat test.txt
3 commit

$ git log --oneline --all --graph --decorate
* 6a7ace8 (HEAD -> master) 3 commit
* 3be5879 2 commit
* 3d1a4d4 1 commit

回退两个版本

$ git reset --hard HEAD^^
HEAD is now at 3d1a4d4 1 commit

$ cat test.txt
1 commit

$ git log --oneline --all --graph --decorate
* 3d1a4d4 (HEAD -> master) 1 commit

回退到最新的那个版本(回去了)

$ git reflog
3d1a4d4 HEAD@{0}: reset: moving to HEAD^^
6a7ace8 HEAD@{1}: reset: moving to HEAD^
1efcf18 HEAD@{2}: commit: 4 commit
6a7ace8 HEAD@{3}: commit: 3 commit
3be5879 HEAD@{4}: commit: 2 commit
3d1a4d4 HEAD@{5}: commit (initial): 1 commit

$ git reset --hard 1efcf18
HEAD is now at 1efcf18 4 commit

xiaol@xiaol-PC MINGW64 /d/code/git_test (master)
$ cat test.txt
4 commit

$ git log --oneline --all --graph --decorate
* 1efcf18 (HEAD -> master) 4 commit
* 6a7ace8 3 commit
* 3be5879 2 commit
* 3d1a4d4 1 commit

 

posted @ 2019-03-20 21:04  413Xiaol  阅读(131)  评论(0编辑  收藏  举报