工作目录回滚:
[root@zxw6 zxw]# echo "zhao" >> test
[root@zxw6 zxw]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@zxw6 zxw]# git checkout -- test
[root@zxw6 zxw]# cat test
zhaoxiaowei
暂存区域回滚:
[root@zxw6 zxw]# echo "zhao" >> test
[root@zxw6 zxw]# git add . 提交到暂存区域
[root@zxw6 zxw]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: test
#
[root@zxw6 zxw]# git reset HEAD test
Unstaged changes after reset:
M test
[root@zxw6 zxw]# cat test
zhaoxiaowei
zhao
[root@zxw6 zxw]# git status 回滚到工作目录
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@zxw6 zxw]# git checkout -- test 再次回滚
[root@zxw6 zxw]# cat test
zhaoxiaowei
仓库回滚:
[root@zxw6 zxw]# echo "zhao" >> test
[root@zxw6 zxw]# git add . 提交到暂存
[root@zxw6 zxw]# git commit -m "v4" 提交到仓库
[master d837a86] v4
1 file changed, 1 insertion(+)
[root@zxw6 zxw]# git log
commit d837a868493c2209f0caa303a8339c9caed34ba2
Author: Your Name <you@example.com>
Date: Mon Jul 15 07:56:18 2019 -0400
v4
commit c866a1da7133f5c7fc04ccc6bc862f7e4dee478f
Author: Your Name <you@example.com>
Date: Mon Jul 15 07:18:45 2019 -0400
v1
[root@zxw6 zxw]# git reset --hard c866a1da713
HEAD is now at c866a1d v1
[root@zxw6 zxw]# git log
commit c866a1da7133f5c7fc04ccc6bc862f7e4dee478f
Author: Your Name <you@example.com>
Date: Mon Jul 15 07:18:45 2019 -0400
v1
[root@zxw6 zxw]# cat test
Zhaoxiaowei
查看所有版本:
[root@zxw6 zxw]# git reflog
c866a1d HEAD@{0}: reset: moving to c866a1da713
d837a86 HEAD@{1}: commit: v4
c866a1d HEAD@{2}: reset: moving to c866a1da7133
f38ef58 HEAD@{3}: commit: v3
a81565c HEAD@{4}: commit: v2
c866a1d HEAD@{5}: commit (initial): v1
[root@zxw6 zxw]# git reset --hard f38ef58
HEAD is now at f38ef58 v3
[root@zxw6 zxw]# git log
commit f38ef582124460bebb4e9113c9abfa4a3195e69c
Author: Your Name <you@example.com>
Date: Mon Jul 15 07:28:29 2019 -0400
v3
commit a81565c30c5a39f0bf3a309cb52bbd75942e5bdc
Author: Your Name <you@example.com>
Date: Mon Jul 15 07:27:55 2019 -0400
v2
commit c866a1da7133f5c7fc04ccc6bc862f7e4dee478f
Author: Your Name <you@example.com>
Date: Mon Jul 15 07:18:45 2019 -0400
v1