简体   繁体   English

Git-意外git checkout特定文件后如何从文件中恢复代码更改

[英]Git - How to recover code changes from file after accidentally git checkout particular file

I accidentally did checkout file. 我不小心做了结帐文件。 And that file had code changes. 该文件已更改代码。

So Can I recover that changes ? 那么我可以恢复这些更改吗?

Contrary to what people leaving comments would have you believe, a path-specific checkout (ie git checkout path/to/my/file ) would indeed overwrite your unstaged changes in that file without warning, because that is exactly the command git expects you to use if you intend to undo unstaged changes. 与人们发表评论的想法相反,您会相信,特定于路径的签出(即git checkout path/to/my/file )确实会覆盖该文件中未分段的更改而不会发出警告,因为这正是git期望您执行的命令如果您打算撤消未进行的更改,则使用。

And unfortunately, because the changes were unstaged, git has no history of them. 不幸的是,由于更改是未进行的,因此git没有历史记录。 While git is very good at retaining history once it's been committed, it offers basically no special protection for the work tree; 尽管git在提交后非常擅长保留历史记录,但它基本上没有为工作树提供任何特殊保护; so commands affecting untracked files or files with unstaged changes are particularly dangerous. 因此,影响未跟踪文件或具有未暂存更改的文件的命令特别危险。

If you take continuous backups (or are lucky and took a backup while the changes existed), you could use those. 如果您进行连续备份(或者很幸运并且在存在更改的情况下进行了备份),则可以使用这些备份。 Some IDE's keep a work history that might help. 某些IDE保留可能有用的工作历史记录。 But unfortunately, in most cases (unless you have unusually solid backup practices) there probably isn't much to be done in this scenario. 但是不幸的是,在大多数情况下(除非您有非常规的可靠备份做法),在这种情况下可能没有太多事情要做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM