如何在 git rm 并推送到 github 后取消删除文件?

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

How do I undelete a file after git rm and pushing to github?

gitgithubrepositoryundelete

提问by Alex Chao

I recently cloned a repository on my local machine and then did a git remove on one of the files and pushed those changes back to the github repository. My question is how do I restore that file back on the original github repository?

我最近在本地机器上克隆了一个存储库,然后对其中一个文件进行了 git remove 操作,并将这些更改推送回 github 存储库。我的问题是如何将该文件恢复到原始 github 存储库中?

回答by acj

If you can find a previous commit abcdthat has the deleted file, then you can use

如果您可以找到abcd具有已删除文件的先前提交,则可以使用

git checkout abcd file-to-restore

git checkout abcd file-to-restore

to restore it. You'll need to commit the file again.

恢复它。您需要再次提交文件。

回答by mevdschee

Let's assume the file "undelete.sh" was accidentally removed.

假设文件“undelete.sh”被意外删除。

Then get the hash of the commit in which this file is deleted:

然后获取删除此文件的提交的哈希:

git rev-list -n 1 HEAD -- undelete.sh

Which gives you the hash of the deletion:

这为您提供了删除的哈希值:

ae85c23372a8a45b788ed857800b3b424b1c15f8

Now you can checkout the version of the file before the deletion:

现在您可以在删除前检出文件的版本:

git checkout ae85c23372a8a45b788ed857800b3b424b1c15f8^ -- undelete.sh

And you should have the file back. You may add, commit and push it to the repository.

你应该把文件拿回来。您可以添加、提交并将其推送到存储库。

(source)

来源

回答by user1179442

1.If the deleted file is in your .gitignore,then you can remove it in .gitignoreand git addit again.

1.如果删除的文件在您的文件中.gitignore,则您可以将其删除.gitignoregit add再次删除。

2.You can just use git reset 'commit id contains your deleted file'then merge and push it again.

2.您可以使用git reset 'commit id contains your deleted file'然后合并并再次推送它。

回答by Fishdou

You should use git reset HEAD~and then use git checkout -- <filename>to restore deleted files.

您应该使用git reset HEAD~然后使用git checkout -- <filename>来恢复已删除的文件。