在 Git 中,如何恢复在提交之前恢复的暂存文件? [英] In Git, how can I recover a staged file that was reverted prior to committing?

查看:105
本文介绍了在 Git 中,如何恢复在提交之前恢复的暂存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 Git Tower 将更改拉入我的存储库.当我这样做时,发生了冲突,我错误地点击了所有阶段(因为我想在解决冲突后提交).当我这样做时,冲突将自己标记为已解决.

我想手动解决更改,所以我点击了中止合并",但是,当我这样做时,它回滚了我的所有更改!有什么办法让他们回来吗?

解决方案

如果您将任何内容暂存到 git,您可能应该能够恢复它.(如果你只是改变了工作副本,你将无法恢复它.)

首先:不要运行git gc.在继续之前备份您的存储库和工作副本.(确保备份 .git 目录.)同时避免关闭发生这种情况的终端,和/或重新启动——如果一切都失败了,你有机会在历史/内存中找到东西.

无论如何,首先要尝试的是:

<前>git fsck --lost-found

它会打印出类似的东西

<前>检查对象目录:100% (256/256),完成.检查对象:100% (30165/30165),完成.悬垂的斑点 8f72c7d79f964b8279da93ca8c05bd685e892756悬空提交 4993502a6394491190d3f4d6fb3d1e14019c2e9b

由于您丢失了暂存文件并且没有进行提交,您对 dangling blob 条目感兴趣.

为每个文件运行 git show <sha> - 其中一些应该是您的文件.

I was attempting to pull a change into my repository using Git Tower. When I did so, there was a conflict and I mistakenly hit stage all (as I wanted to commit after resolving the conflict). When I did so, the conflict marked itself as resolved.

I wanted to manually resolve the change so I hit "Abort Merge", however, when I did this, It rolled back all my changes! Is there any way to get them back?

解决方案

If you had anything staged to git, you probably should be able to get that back. (If you just changed working copy, you wouldn't be able to restore it.)

First of all: do not run git gc. Backup your repository and working copy before going ahead. (Make sure to backup .git directory.) Also avoid closing terminal where this happened, and/or rebooting — if all fails, you have a chance to find stuff in history / memory.

Anyway, first thing to try is:

git fsck --lost-found

It will print something like

Checking object directories: 100% (256/256), done.
Checking objects: 100% (30165/30165), done.
dangling blob 8f72c7d79f964b8279da93ca8c05bd685e892756
dangling commit 4993502a6394491190d3f4d6fb3d1e14019c2e9b

Since you lost staged files and did not do a commit, you're interested in dangling blob entries.

Run git show <sha> for each one — some of them should be your files.

这篇关于在 Git 中,如何恢复在提交之前恢复的暂存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆