简体   繁体   English

git pull 后恢复丢失的文件

[英]Recovering lost files after git pull

I added some old assignments to my GitHub.我在我的 GitHub 上添加了一些旧作业。 I added one assignment from my local repo to the remote successfully, but made the mistake of adding a README.md to it on my web browser.我成功地将本地存储库中的一项分配添加到了远程,但是在我的网络浏览器上错误地添加了README.md This caused me to not be able to push any commits locally anymore because这导致我无法再在本地推送任何提交,因为

Updates were rejected because the remote contains work that you do
not have locally. This is usually caused by another repository pushing
to the same ref. You may want to first integrate the remote changes
(e.g., 'git pull ...') before pushing again.

So I did a git pull origin master command to get that README.md file from GitHub, but I believe when it merged with my local branch, it deleted all of my local files that I had not pushed yet.所以我执行了一个git pull origin master命令从 GitHub 获取该README.md文件,但我相信当它与我的本地分支合并时,它会删除我尚未推送的所有本地文件。 Now all of my other assignments are just empty folders.现在我所有的其他任务都只是空文件夹。 How do I get these files back?如何取回这些文件? And what would have been the better alternative to pulling from the remote repository?从远程存储库中提取的更好的选择是什么?

You cannot get file README.md at local.您无法在本地获取文件README.md Because this file never staged to local repository.因为此文件从未暂存到本地存储库。

You can get file README.md at GitHub.com easily, because you just saved it, it has a commit hash string.您可以在 GitHub.com 轻松获取文件README.md ,因为您刚刚保存了它,它有一个提交哈希字符串。

How do I get these files back如何取回这些文件

Actions performed with git are undoable only to the extent that there is a commit reflecting the state you want to get back to.使用 git 执行的操作仅在有反映您想要返回的状态的提交时才可撤消。 Only a commit is a "thing" in git.只有提交才是 git 中的“事物”。

So, if you did a git pull and it had effects you don't like, you can git reset back to the commit before you did the pull.所以,如果你做了一个git pull并且它有你不喜欢的效果,你可以在你做 pull 之前git reset回到提交。

But if your action affected files that were not committed, then there is absolutely nothing you can do.但是,如果您的操作影响了提交的文件,那么您绝对无能为力。 Files that are not committed are no concern of git's.未提交的文件与 git 无关。

您可以在 GIT > Shelf 中恢复文件,如果您使用 Intellij,很容易找到您未提交的更改。

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

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