3 回答
![?](http://img1.sycdn.imooc.com/5333a0aa000121d702000200-100-100.jpg)
TA贡献1796条经验 获得超4个赞
打开git库文件,可以看到文件夹内的所有文件都没了,只有一个git仓库还存在。
这时在文件夹上右击,在弹出的菜单中选择"Git Bash Here"这一项。
进入Bash页面后,我们执行ls操作,可以发现,文件夹下同样不存在文件。
这时我们再执行git reflog。reflog它会记录所有HEAD的历史,也就是说当你做 reset,checkout等操作的时候,这些操作会被记录在reflog中。
如果我们要找回文件,只需要做如下操作(*表示上一步中出现的log号):
git reset --hard *
我们再次执行ls操作,可以发现文件已经还原回来了.
所以,如果因为reset等操作丢失一个提交的时候,你总是可以把它找回来。
![?](http://img1.sycdn.imooc.com/54584eff000195a302200220-100-100.jpg)
TA贡献1802条经验 获得超4个赞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 首先输入 git fsck --lost-found
会看到 一条一条的记录 类似 dangling commit 7010e0447be96627fde29961d420d887533d7796
复制dangling commit 的id(其他的dangling blob不用理会) 然后输入 git show 7010e0447be96627fde29961d420d887533d7796
查看具体内容, 找到你想要的记录 记录中会描述日期和摘要, 日期是你git stash 的日期, 摘要会记录你是在哪一条commit 上进行git stash操作的, 类似(WIP on integration-xf: 2e205ac Merge branch 'release' into develop) 貌似只能一条记录一条记录的查看
找到你想要的记录后输入 git merge 7010e0447be96627fde29961d420d887533d7796
这样就还原了你git stash drop, git stash clear 的内容 |
![?](http://img1.sycdn.imooc.com/545868b60001587202200220-100-100.jpg)
TA贡献1780条经验 获得超5个赞
首先git status一把,看看此时工作区的状态
[xxx@xxx static_files]$ git status
# On branch master
nothing to commit (working directory clean)123
可见此时没有任何修改的内容。
再看看具体有什么
xxx@xxx static_files]$ ls
abbr_data breakfast_data room_type_data12
此时总计有三个文件。OK,让我们干掉其中一个
[xxx@xxx static_files]$ git rm abbr_data
rm 'static_files/abbr_data'
[xxx@xxx static_files]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: abbr_data
#
[xxx@xxx static_files]$ ls
breakfast_data room_type_data1234567891011
此时工作区的文件就只剩两个了,abbr_data这个文件,已经被我们干掉。
如果我们想要恢复,怎么办呢?
[xxx@xxx static_files]$ git checkout -- abbr_data
error: pathspec 'static_files/abbr_data' did not match any file(s) known to git.12
直接checkout,是不行的。
那怎么办呢?其实在git status中,已经告诉我们怎么办了。
[xxx@xxx static_files]$ git reset HEAD abbr_data
Unstaged changes after reset:
M static_files/abbr_data123
用reset命令,先将abbr_data这个文件找回来。
[xxx@xxx static_files]$ git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: abbr_data
#
no changes added to commit (use "git add" and/or "git commit -a")123456789
再checkout一把
[xxx@xxx static_files]$ git checkout -- abbr_data
[xxx@xxx static_files]$12
看到checkout以后没有任何提示,这事就成了。因为git的哲学跟unix的哲学一样,没消息就是最好的消息。。。
再ls一下,果然,abbr_data找回来了。
[xxx@xxx static_files]$ ls
abbr_data breakfast_data room_type_data
- 3 回答
- 0 关注
- 2025 浏览
添加回答
举报