转自:https://www.liaoxuefeng.com/wiki/896043488029600/897013573512192
1.reset命令 --hard参数
首先查看commit记录,
myproj % git log commit 7ce989d2294aeede765a4792d40a3df9d0844355 (HEAD -> master) Author: bg <> Date: Sat Aug 20 17:52:51 2022 +0800 修改a.txt,+0820 bg修改一下 commit c3a96ee4f0c6439ffda1b8b3bfbfd2e7f9a730fa Author: bg Date: Sat Aug 20 16:34:29 2022 +0800 m a.txt, add b.txt commit 594e046e8966ff38ad404791edb66f5a6f1e9e4d (origin/master, feature/1) Author: Date: Wed Aug 10 00:20:40 2022 +0800 initial commit
使用命令
git reset --hard HEAD^ //或者 git reset --hard HEAD~1
表示回退到上1个版本,HEAD~n表示回退到上n个版本, 回退的本质是移动HEAD指针,git reset 命令是在更改git指针HEAD的指向,具体是更改指向不同的版本号。然后顺便把工作区的文件更新了。
类似的:
那么回退到上一次的版本:
myproj % git reset --hard head~1 HEAD 现在位于 c3a96ee m a.txt, add b.txt yproj % git log commit c3a96ee4f0c6439ffda1b8b3bfbfd2e7f9a730fa (HEAD -> master) Author: bg <MacBook-Pro-2.local> Date: Sat Aug 20 16:34:29 2022 +0800 m a.txt, add b.txt commit 594e046e8966ff38ad404791edb66f5a6f1e9e4d (origin/master, feature/1) Author: Date: Wed Aug 10 00:20:40 2022 +0800 initial commit
可以发现log里只有前两次的提交了,如果想恢复了,直接再reset之前的commitID前几位即可,
myproj % git reset --hard 7ce989d2294 HEAD 现在位于 7ce989d 修改a.txt,+0820 bg修改一下
logs/有记录所有的命令情况,可以找回:
myproj % git reflog 7ce989d (HEAD -> master) HEAD@{0}: reset: moving to 7ce989d2294 c3a96ee HEAD@{1}: reset: moving to head~1 7ce989d (HEAD -> master) HEAD@{2}: checkout: moving from feature/1 to master 594e046 (origin/master, feature/1) HEAD@{3}: checkout: moving from master to feature/1 7ce989d (HEAD -> master) HEAD@{4}: commit: 修改a.txt,+0820 bg修改一下
2.--soft参数
标签:reset,HEAD,Git,git,master,版本,回退,commit,txt From: https://www.cnblogs.com/BlueBlueSea/p/16608959.html