从本地存储库中删除它们后,如何从远程 git 存储库中获取 files/folders?

How to get files/folders from a remote git repository after we delete them from our local repository?

我从本地存储库中删除了几个文件夹和文件我刚刚选择并删除了它们。 (包括 'images' 、 'config' 等文件夹和一些由我修改但不再需要的文件)

我尝试 git pull upstream mastergit fetch upstream master 将这些文件和文件夹返回到我的本地存储库。

我做的基本上是这样的,

  1. 我从 git 获得了一个存储库并将其放置在本地
  2. 我在本地对一些文件和文件夹进行了一些更改
  3. 我想要一些文件有旧版本(我在第一步之后的版本)
  4. 我想删除文件并取回它们并从 git 重新获得新副本(使用上述命令,不幸的是它们不起作用)
  5. 但我可以创建一个新的本地存储库并克隆原始存储库。但工作量太大,而且我还会丢失对剩余文件所做的更改。

*I have used SVN and there I can delete any file from the repo and when I get an update the file will be back. But I don't know how to do that with GIT.*

那么有什么方法可以安全地取回那些已删除的文件并拥有这些新副本。?

注意:我没有提交删除内容

首先使用git status查看您删除了哪些文件,然后使用git checkout filename_or_dir_name恢复它们。

例如:

# atupal at atupal in /tmp/tmp [18:15:39]
$ git status .
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  deleted:    a

no changes added to commit (use "git add" and/or "git commit -a")
# atupal at atupal in /tmp/tmp [18:15:41]
$ ls           
# atupal at atupal in /tmp/tmp [18:15:47]
$ git checkout .
# atupal at atupal in /tmp/tmp [18:15:51]
$ ls         
a
# atupal at atupal in /tmp/tmp [18:15:55]
$ 

如果您使用 git add file_name 暂存修改过的文件,则在 git-checkout 之后恢复的文件是 "fresh",其他则不是。