当前位置:   article > 正文

reset --hard 后如何找回_git hard回退的代码找回

git hard回退的代码找回

:reset --hard 后如何找回

:拢共分两步

一:找到你想要切回的commit-id

二:切过去

————————————————————————————————————

一:找到你想要切回的commit-id

你的git操作在log中都会有记录

.git/logs/refs/heads/master

如:

1:首先执行 git clone --> git log

  1. ***@***:~/GIT/StudyGit$ git log --oneline -5
  2. 6a851f1 (HEAD -> master, origin/master, origin/HEAD) update main.cpp.
  3. 35a96a6 add 1.txt
  4. d57b460 Merge branch 'master' of https://gitee.com/qiaoyuhu/StudyGit
  5. ebe2a1b add amend.test add amend.test to test git commit --amend add make.sh
  6. fe4c3e5 qiong add condition.cpp

2:执行修改一个文件后执行 git commit --amend

  1. ***@***:~/GIT/StudyGit$ git log --oneline -5
  2. 9933cb9 (HEAD -> master) update main.cpp.
  3. 35a96a6 add 1.txt
  4. d57b460 Merge branch 'master' of https://gitee.com/qiaoyuhu/StudyGit
  5. ebe2a1b add amend.test add amend.test to test git commit --amend add make.sh
  6. fe4c3e5 qiong add condition.cpp

3:执行 git reset --hard HEAD~

  1. ***@***:~/GIT/StudyGit$ git reset --hard HEAD~
  2. HEAD is now at 35a96a6 add 1.txt
  3. ***@***:~/GIT/StudyGit$ git log --oneline -5
  4. 35a96a6 (HEAD -> master) add 1.txt
  5. d57b460 Merge branch 'master' of https://gitee.com/qiaoyuhu/StudyGit
  6. ebe2a1b add amend.test add amend.test to test git commit --amend add make.sh
  7. fe4c3e5 qiong add condition.cpp
  8. 3ea8ae6 add amend.test add amend.test to test git commit --amend

问题:假设命令行里已经找不到git commit id 的情况下,如何才能找到commit --amend 前的代码呢?

  1. ***@***:~/GIT/StudyGit$ cat .git/logs/refs/heads/master
  2. 0000000000000000000000000000000000000000 6a851f16798a0a22e0a7d93891ed8af8f0d74126 qiaoyuhu <qiaoyuhu@axera-tech.com> 1665715932 +0800 clone: from https://gitee.com/qiaoyuhu/StudyGit.git
  3. 6a851f16798a0a22e0a7d93891ed8af8f0d74126 9933cb919ab0fe6702e852375a6085fec74e4020 qiaoyuhu <qiaoyuhu@axera-tech.com> 1665716114 +0800 commit (amend): update main.cpp.
  4. 9933cb919ab0fe6702e852375a6085fec74e4020 35a96a66f135914638f298a75a20a6b5c4f6e4cc qiaoyuhu <qiaoyuhu@axera-tech.com> 1665716183 +0800 reset: moving to HEAD~

这个文件就是你所有操作的记录

解读如下:

记录1: clone操作 ,commit id 从 (0000——> 6a851f1)

记录2:commit (amend)操作,commit id 从(6a851f1——>9933cb9)

记录3:reset 操作,commit id 从(9933cb9——>35a96a6)

二:切过去

剩下的你想切到哪次提交,直接git checkout commit-id 就行

git checkout 6a851f1

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/465282
推荐阅读
相关标签
  

闽ICP备14008679号