简体   繁体   English

如何使用 git 恢复已删除的代码文件

[英]How to restore the deleted code files with using git

I was developing a web application.我正在开发一个 web 应用程序。 I also used Git with it.我还使用了 Git。 For approximately one month, I didn't push to Github.大约有一个月的时间,我没有推送到 Github。 During my attempt to push updated code today, I found a file with a memory size of approximately 500 MB.在我今天尝试推送更新代码的过程中,我发现了一个 memory 大小约为 500 MB 的文件。 It shows the error that the file is too large for Github.它显示文件对于 Github 太大的错误。 Therefore, I tried removing that file from the commit in the staging area.因此,我尝试从暂存区域的提交中删除该文件。 I then added that file to.gitignore.然后我将该文件添加到.gitignore。 Following that, I tried git reset and git trash .之后,我尝试了 git 重置git 垃圾 However, they did not work.然而,他们没有工作。 Using the command git add and then git commit with the updated commit.使用命令 git 添加然后 git 提交和更新的提交。 The file is still listed there.该文件仍列在那里。 I found the command git reset --hard 'origin/main' on StackOverflow.我在 StackOverflow 上找到了命令git reset --hard 'origin/main' The command reverted the previous commit, and I lost all my code.该命令恢复了先前的提交,我丢失了所有代码。 The git logs do not show any updated commits. git 日志不显示任何更新的提交。 Would it be possible to find that code there?是否有可能在那里找到该代码?

I added the screenshot from git reflog .我添加了来自git reflog的屏幕截图。 git reflog

When I run the below command, I need the files before Update after forecasting.当我运行以下命令时,我需要在预测后更新之前的文件。 It shows the error attached in the screenshot.它显示了屏幕截图中附加的错误。

git checkout 950362c -b restored_branch git 结帐 950362c -b restore_branch

在 git 结帐之后

git reflog shows a kind of history of the actions you performed in git. git reflog显示了您在 git 中执行的操作的一种历史记录。 Each linehas the commit hash and what you did there.每行都有提交 hash 和你在那里做了什么。

The first line shows that you had commit 6a49195 and performed a reset on it.第一行显示您已提交6a49195并对其执行了重置。 Assuming git hasn't GCed yet (and if you didn't do anything else since it shouldn't have), this commit should still exist, even though its no longer associated with any branch, and you should be able to check it out:假设 git 还没有 GCed(如果你没有做任何其他事情,因为它不应该有),这个提交应该仍然存在,即使它不再与任何分支关联,你应该能够检查它:

git checkout 6a49195 -b restored_branch

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

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