感谢廖雪峰老师
来源就是这里啦
总结:
- git log 查看提交历史,确定回退位置,到过去
- git reflog 查看命令历史,确定恢复位置 ,回未来
- 以上两个命令可以得到你想要的关键信息:commit id
- HEAD指向当前版本
- git reset --hard commit id,是回退和恢复的命令,commit id 可以是版本号,也可以是HEAD^等
具体过程:
- 修改readme文件,将其提交
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git add readme.txt
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git commit -m"second change"
[master cdfa377] second change
1 file changed, 2 insertions(+), 1 deletion(-)
- 再次修改readme.txt内容,再将其提交到仓库
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git diff
diff --git a/readme.txt b/readme.txt
index d88bf0c..2a1df67 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,4 @@
yuwxl is fool happy,now!
hhh
-i am coming!
\ No newline at end of file
+i am coming!
+i am coming again!!
\ No newline at end of file
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git add readme.txt
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git commit -m"third change"
[master a451f4c] third change
1 file changed, 2 insertions(+), 1 deletion(-)
- git log 查看提交日志
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git log
commit a451f4cc9dd06949a0d806d5f0e492abe25013de (HEAD -> master)
Author: yuwxl <yuwxl@qq.com>
Date: Sun Mar 8 19:25:16 2020 +0800
third change
commit cdfa377b6ace4b8484fa4bda24fff79d64a55dd1
Author: yuwxl <yuwxl@qq.com>
Date: Sun Mar 8 19:21:09 2020 +0800
second change
commit b7fc6a4354cc02e32441241d7300b0cbc3f4e601
Author: yuwxl <yuwxl@qq.com>
Date: Fri Mar 6 19:53:52 2020 +0800
a little change readme
commit 6fb748f04be5018999effaf40d949b917e0948d9
Author: yuwxl <yuwxl@qq.com>
Date: Fri Mar 6 19:45:31 2020 +0800
yuwxl first test git
:
- 加上 --pretty=oneline参数让结果更简洁
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git log --pretty=oneline
a451f4cc9dd06949a0d806d5f0e492abe25013de (HEAD -> master) third change
cdfa377b6ace4b8484fa4bda24fff79d64a55dd1 second change
b7fc6a4354cc02e32441241d7300b0cbc3f4e601 a little change readme
6fb748f04be5018999effaf40d949b917e0948d9 yuwxl first test git
- git reset 命令回退版本
- HEAD^回退一个, HEAD ^ ^回退两个,HEAD~n回退n 个
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git reset --hard HEAD^
HEAD is now at cdfa377 second change
- 此时查看readme.txt已经回退到上一个版本了
- 在该窗口未关闭情况下,根据版本号可以恢复刚刚的回退,版本号不必写全
k'k@DESKTOP-E1NTNUS MINGW64 /d/yuwxl (master)
$ git reset --hard a451f
HEAD is now at a451f4c third change
- git reflog记录每次命令,根据此命令的commit id可以确定回到未来的哪个版本(万一不小心关掉了窗口又后悔刚刚的回退啦)
$ git reflog
a451f4c (HEAD -> master) HEAD@{0}: reset: moving to a451f
cdfa377 HEAD@{1}: reset: moving to HEAD^
a451f4c (HEAD -> master) HEAD@{2}: commit: third change
cdfa377 HEAD@{3}: commit: second change
b7fc6a4 HEAD@{4}: commit: a little change readme
6fb748f HEAD@{5}: commit (initial): yuwxl first test git
志不强者,智不达。
——墨翟
来源:CSDN
作者:拿云yuwxl
链接:https://blog.csdn.net/qq_43855740/article/details/104739244