git checout 失误后回溯

一天代码写完一定要走 git add git commit

有时候失误 git checkout xxx 源文件丢失怎么办??

理解:当前完成品代码 切换到上个版本了,完成品代码的信息不在git log内。

cpp 复制代码
查看所有分支的提交历史,包括已经被删除的提交记录。
git reflog 

0663723 (HEAD, origin/master, master) HEAD@{0}: checkout: moving from master to 0663723
0663723 (HEAD, origin/master, master) HEAD@{1}: checkout: moving from b895ee45673f5e4e4d6a778253cd736a43f67d51 to master
b895ee4 HEAD@{2}: commit: 界面美化   **成品代码文件 丢失的文件
0663723 (HEAD, origin/master, master) HEAD@{3}: checkout: moving from master to 0663723fb8293852d83dfe56173d37d555eaaf7b
0663723 (HEAD, origin/master, master) HEAD@{4}: commit: 完善界面 增加日志
3780ba2 HEAD@{5}: commit: 基本界面完成
7751245 HEAD@{6}: commit: 基本界面v2.0 基本排版
6bf13dd HEAD@{7}: commit: 基本界面
539d8e6 HEAD@{8}: commit (initial): 登录模块

git show hash 查看修改文件的记录 确定到完成品代码

找到了后比如 b895ee4 
git checkout b895ee4   //这就回到了完成品代码

git checkout master       //但是主分支不是b895ee4 使用切回去 然后再重新设置。
git reset --hard b895ee4 
git push --force origin master   //推送给远端

总结:主要还是记录一下git方法,由于是新手,有时候不知不觉的就把成品代码文件回溯到之前版本。导致一周工作量白费

相关推荐
Gary Studio1 小时前
Git vscode 插件推荐
ide·git·vscode
StackNoOverflow2 小时前
IDEA + Git + Gitee 全流程实战:从安装、提交到解决冲突
git·gitee·intellij-idea
淘矿人15 小时前
从0到1:用Claude启动你的第一个项目
开发语言·人工智能·git·python·github·php·pygame
lpfasd12316 小时前
Git/Gitee/GitHub 3 个安全凭证详解
git·gitee·github
李日灐20 小时前
< 7 > Linux 开发工具:git 版本控制器 和 cgdb/gdb 调试器
linux·运维·服务器·开发语言·git·调试器·gdb/cgdb
Gust of wind20 小时前
idea结合git和Gitee的初步使用
git·gitee·intellij-idea
夜七少eleanor20 小时前
【Git】2026全图文详解安装教程
git
海边的Kurisu20 小时前
从零开始的Git生活 | 刚实习同学的噩梦 And 参与开源不可缺的一环
git·生活
不老刘1 天前
Git Cherry-Pick:微前端架构下的“精准医疗”与最佳实践
前端·git
爬楼的猪1 天前
Git Folder Dashboard
git