git git可以撤消未暂存文件的签出吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2689265/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:17:40  来源:igfitidea点击:

Can git undo a checkout of unstaged files

git

提问by Herr W.

I accidentially discard my changes on files in my local working tree via git checkout. The files aren't staged at this time. Is it posible to "undo" this checkout?

我不小心通过 git checkout 放弃了对本地工作树中文件的更改。目前未暂存这些文件。是否可以“撤消”此结帐?

采纳答案by VonC

I believe if a file is modified but not yet added (staged), it is purely "private".
Meaning it cannot be restored by GIT if overwritten with the index or the HEAD version (unless you have a copy of your current work somewhere)."

我相信如果文件被修改但尚未添加(暂存),则它纯粹是“私有的”。
这意味着如果被索引或 HEAD 版本覆盖,则 GIT 无法恢复它(除非您在某处有当前工作的副本)。”

A "private" content is one only visible in your current directory, but not registered in any way in Git.

“私有”内容仅在您的当前目录中可见,但未在 Git 中以任何方式注册。

回答by Christoph Geschwind

If you are using a "professional" IDE chances are good that you can restore files from a local History. In Rubymine for example you can right click files and watch a history of changes independent from the git changes, saved me a few times now ^^

如果您使用的是“专业”IDE,那么您很有可能从本地历史记录中恢复文件。例如,在 Rubymine 中,您可以右键单击文件并查看独立于 git 更改的更改历史记录,现在为我节省了几次 ^^

回答by roman-roman

If you are working in an editor like Sublime Text, and have file in question still open, you can press ctrl+z, and it will return to the state it had before git checkout.

如果你在像 Sublime Text 这样的编辑器中工作,并且有问题的文件仍然打开,你可以按 ctrl+z,它会返回到它在 git checkout 之前的状态。

回答by Marcin Gil

Unfortunately your changes are lost. Your private modifications are simply overwritten. Unless you did git stashprior making checkout...

不幸的是,您的更改丢失了。您的私人修改将被简单地覆盖。除非你git stash在结账之前做过...

Take it from the brighter side: you can now implement things even better ;)

从更光明的一面来看:您现在可以更好地实施;)

回答by Marcin Szymczak

Check local history in your IDE.

在 IDE 中检查本地历史记录。

回答by Benjohn

Developing on OS X?Using Xcode?You're likely to be in luck!

OS X开发?使用Xcode?你很可能走运!

As described in a comment by qungu, OS X maintains an autosaved version history of files, even if you're not using time machine.

正如qungu评论中所述,OS X 会维护文件的自动保存版本历史记录,即使您没有使用 time machine

So, if you've blown away your unstaged local changes with a careless git checkout ., here's how you can probablyrecover all your work.

所以,如果你吹走满不在乎的不分阶段的局部变化git checkout .,这里是你如何能可能恢复所有的工作。

If somebody finds this thread having destroyed some work in XCode, there is a way to get the AutoSave history. XCode itself does not have a menu entry to see the AutoSave history, but it does store it. If you open the files in question in TextEdit, you can revert and look through the AutoSave history under File > Revert.

如果有人发现此线程破坏了 XCode 中的某些工作,则有一种方法可以获取自动保存历史记录。XCode 本身没有用于查看自动保存历史记录的菜单项,但它确实存储了它。如果您在 TextEdit 中打开有问题的文件,您可以还原并查看文件 > 还原下的自动保存历史记录。

Which is awesome, and recovered about a day of work for me, yesterday.

这太棒了,昨天为我恢复了大约一天的工作。



You might ask, "Why doesn't the git command-line UI, the premier VCS used for software engineering in 2016201720182019, at least back up files before just blowing them away? Like, you know, well written software tools for about the last three decades."

您可能会问,“为什么 git 命令行 UI,用于软件工程的首要 VCS2016年2017年2018年2019 年,至少在将文件吹走之前备份文件?就像,你知道,在过去的三十年里写得很好的软件工具。”

Or perhaps you ask, "Why is this insanely awesome file history feature accessible in TextEdit but not Xcode where I actually need it?"

或者你可能会问,“为什么这个非常棒的文件历史功能可以在 TextEdit 中访问,但在我真正需要的地方却不能访问 Xcode?”

… and both of those, I think, will tell you quite a lot about our industry. Or maybe you'll go and fix those tools. Which would be super.

......我认为,这两个都会告诉你很多关于我们行业的信息。或者也许你会去修理这些工具。这将是超级的。

回答by ishab acharya

In VSCODE ctrl+z (undo) worked for me

在 VSCODE ctrl+z (undo) 中对我有用

I did git checkout .instead of git add .and all my file changes were lost.

我做了git checkout .而不是git add .我所有的文件更改都丢失了。

But Now using command + zin my mac , recovered the changes and saved a tone of work for me.

但是现在command + z在我的 mac 中使用,恢复了更改并为我节省了工作量。

回答by iksa

In case you ever stashed the changes earlier (for example, prior to rebasing), this will likely help

如果您之前曾隐藏更改(例如,在变基之前),这可能会有所帮助

How to recover a dropped stash in Git?

如何在 Git 中恢复丢失的存储?

even if you have already 'stash pop'ed the changes.

即使您已经“隐藏”了更改。

回答by sscirrus

An effective savior for this kind of situation is Time Machine (OS X) or a similar time-based backup system. It's saved me a couple of times because I can go back and restore just that one file.

这种情况的有效救星是 Time Machine (OS X) 或类似的基于时间的备份系统。它救了我几次,因为我可以返回并恢复那个文件。

回答by Pierre-Verthume Larivière

I just had that happen to me, I checked out an entire folder containing hours of work! Fortunately I found out that my IDE Netbeans keeps an history of each file, that allowed me to recuperate 99% of the stuff even though I needed to fix a few things manually.

我刚刚发生了这种情况,我查看了一个包含工作时间的整个文件夹!幸运的是,我发现我的 IDE Netbeans 保留了每个文件的历史记录,这使我能够恢复 99% 的东西,即使我需要手动修复一些东西。