git 不小心恢复到 master,丢失了未提交的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7147680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accidentally reverted to master, lost uncommitted changes
提问by Trist
I've managed to make a huge blunder (on a Monday morning) Working on Master, forget to create new branch, made changes to files then inadvertently reverted to the master, loosing all updates. I didn't commit the updated files.
我设法犯了一个巨大的错误(在星期一早上)在 Master 上工作,忘记创建新分支,对文件进行更改,然后无意中恢复到 master,丢失了所有更新。我没有提交更新的文件。
Have I lost all updates or can I retrieve them?
我丢失了所有更新还是可以找回它们?
Don't ask me how because every time I think about it I have to punch myself in the face.
不要问我怎么做,因为每次想到它我都必须打自己的脸。
回答by Sailesh
If you had not commited, staged, or stashedthe changes you made, there is no way you can recover those changes.
如果您尚未提交、暂存或隐藏所做的更改,则无法恢复这些更改。
EDIT: Recovering lost changes. Adding this on Mark Longair'ssuggestion (in the comment). This also includes a couple of SO links from his answer below(*), that I found quite informative.
编辑:恢复丢失的更改。将此添加到Mark Longair 的建议中(在评论中)。这还包括他下面的回答(*)中的几个 SO 链接,我发现这些链接非常有用。
If you have ever committedsome change and have lost that commit (like committing in a detached state), you can find that commit using
reflog
. See this SO question*.If you have lost your last stagedchanges, you can also recover that. See this SO question*. (I have never used or tried it myself).
If you have stasheda change, you can also recover that using
pop
orapply
. (I am not sure if the popped/dropped stashes are also recoverable that were not committed). You may find this Recover dropped stash in gituseful.
如果您曾经提交过一些更改并且丢失了该提交(例如在分离状态下提交),您可以使用
reflog
. 看到这个问题*。如果您丢失了上次分阶段的更改,您也可以恢复它。看到这个问题*。(我自己从未使用或尝试过)。
如果您已隐藏更改,您还可以使用
pop
或恢复该更改apply
。(我不确定弹出/丢弃的隐藏是否也可以恢复,但未提交)。你可能会发现这个Recover 在 git 中丢失的存储很有用。
If there are any other methods that anyone can suggest, I'd edit this answer further to add them.
如果还有任何其他人可以建议的方法,我会进一步编辑此答案以添加它们。
回答by Chris Thornton
Two longshots: Some IDEs, such as Delphi, keep an editor history. You may have some recourse there.
Next, if you had your local working directory located in the MyDocuments folder, it may have been automatically backed up by Windows Home Server, Carbonite, MozyPro, etc.. these are usually 'set it and forget it'. maybe you forgot it?
两个远景:某些 IDE,例如 Delphi,会保留编辑器历史记录。你可能有一些追索权。
接下来,如果您的本地工作目录位于 MyDocuments 文件夹中,则它可能已由 Windows Home Server、Carbonite、MozyPro 等自动备份。这些通常是“设置并忘记它”。也许你忘记了?
回答by Vishu
I was stuck in the same situation.
我陷入了同样的境地。
Had a list of the changed files in the terminal before the --hard reset, went into my editor (Sublime 2, doesn't matter though), opened each file and hit cmd+z (undo) once, this effectively undid the changes done by the hard reset and I got my uncommitted changes back :)
在 --hard reset 之前在终端中有一个已更改文件的列表,进入我的编辑器(Sublime 2,不过没关系),打开每个文件并点击 cmd+z(撤消)一次,这有效地取消了更改通过硬重置完成,我得到了我未提交的更改:)
回答by Mark Longair
The critical question here is what you did after making changes to the files. If you created a commit which contained the new state of the files, then you should be able to get them back by looking through the recent entries in git reflog
, finding the SHA1sum of the commit and then creating a new branch from that with git branch recovered <SHA1sum>
, or similar. There's an example of doing this in this answer.
这里的关键问题是您在更改文件后做了什么。如果您创建了一个包含文件新状态的提交,那么您应该能够通过查看 中最近的条目git reflog
,找到提交的 SHA1sum,然后使用git branch recovered <SHA1sum>
或类似的方式创建一个新分支来取回它们。在这个答案中有一个这样做的例子。
If you did git add
on any of the files to stage them, you should also be able to get them back, but this is rather more work - Jakub describes how to do this in this answer.
如果您git add
对任何文件进行了暂存,您也应该能够将它们取回,但这需要更多的工作 - Jakub 在此答案中描述了如何执行此操作。
If you happened to do a git stash
to give yourself a clean status, then of course you can get it back as you would any other stash.
如果你碰巧做了一个git stash
给自己一个干净的状态,那么你当然可以像其他任何藏匿处一样取回它。
Otherwise, I'm afraid that the news is not good.
不然的话,恐怕消息就不好说了。
I hope it's not infuriating to point this out post-hoc, but to just switch back to the master
branch, you shouldn't have needed to use any command that might lose you data - git checkout master
would have told you that you were already on the master branch, and show any uncommitted changes. (Arguably git reset --hard
should have a "Yes, I really mean this" confirmation if there are uncommitted changes, given how often people^W
I lose data this way.)
我希望事后指出这一点不会令人气愤,但只要切换回master
分支,您就不需要使用任何可能会丢失数据的命令 -git checkout master
会告诉您您已经在主分支上,并显示任何未提交的更改。(git reset --hard
如果有未提交的更改,可以说应该有一个“是的,我真的是这个意思”确认,因为我经常以^W
这种方式丢失数据。)
回答by Arafangion
Use the reflog. git reflog
will show you a history of all the commits you have been on, in chronological order.
使用引用日志。git reflog
将按时间顺序向您显示所有提交的历史记录。
If you lost your changes by 'checking out master', then you were probably working headless. git status
will tell you if you are working without a head. (As does git branch
).
如果您通过“签出 master”丢失了更改,那么您可能是在无头工作。 git status
会告诉你是否在无脑工作。(也一样git branch
)。
Working headless isn't that bad (I do it all the time, deliberately), but you will have a greater reliance on the reflog.
无头工作并没有那么糟糕(我一直都是故意这样做的),但是您将更加依赖 reflog。
If you didn't commit your changes, in any way, however, then there is no way to retrieve those files. The only realistic way this could have happened is if you did a hard reset, or explicitly forced a checkout. Do notforce changes unless you are surethat you are comfortable with losing data.
但是,如果您没有以任何方式提交更改,则无法检索这些文件。这可能发生的唯一现实方式是,如果您进行了硬重置,或明确强制结帐。千万不能强行改变,除非你确信你是舒服丢失数据。
Usually, in git, 'forcing' is done by specifying -f
.
通常,在 git 中,“强制”是通过指定-f
.
回答by matthewdaniel
I've done the same thing, more than once unfortunately. Without committing anything git has no idea what you wrote. Even if you did commit then when backward I'm not sure reflex would help.
我做过同样的事情,不幸的是不止一次。没有提交任何东西,git 不知道你写了什么。即使您确实提交了,当向后提交时,我也不确定反射会有所帮助。