简体   繁体   English

从本地存储库中删除文件/文件夹后,如何从远程git存储库中获取文件/文件夹?

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

I deleted several folders and files from the local repository I have just selected them and deleted them. 我从本地存储库中删除了几个文件夹和文件,我刚刚选择它们并删除了它们。 (including the folders like 'images' , 'config' ..etc and some files which have modifications done by me and not needed anymore) (包括“ images”,“ config” .. etc等文件夹以及一些由我完成但不再需要的文件)

I tried git pull upstream master and git fetch upstream master to get those files and folders back to my local repository. 我尝试了git pull upstream mastergit fetch upstream master以将那些文件和文件夹返回到我的本地存储库。

What I did is basically like, 我所做的基本上就像

  1. I got a repository from git and placed it locally 我从git获得了一个存储库并将其放置在本地
  2. I made some changes locally to some files and folders 我在本地对某些文件和文件夹进行了一些更改
  3. I wanted some files to have the older version (what I had after the first step) 我希望某些文件具有较旧的版本(第一步之后的版本)
  4. I thought deleting the files and get them back and have the fresh copy again from the git (using above commands and unfortunately they are not working) 我以为删除文件并找回它们,并再次从git中获得新副本(使用上述命令,很遗憾它们无法正常工作)
  5. But I can create a new local repository and have a clone of original repo. 但是我可以创建一个新的本地存储库,并具有原始存储库的克隆。 But It's too much work and then also I will lost changes I made to the remaining files. 但这工作太多了,然后我将丢失对其余文件所做的更改。

*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.*

So is there any way I can get those deleted files back safely and have those fresh copies.? 那么,有什么办法可以让我安全地恢复那些已删除的文件并拥有这些新副本。

Note : I did not commit the deletions 注意:我没有提交删除

Frist use git status to look what files you have deleted and then use git checkout filename_or_dir_name to resume them. Frist使用git status查看您删除了哪些文件,然后使用git checkout filename_or_dir_name来恢复它们。

eg: 例如:

# 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]
$ 

If you have staged modified files by using git add file_name , then after git-checkout the resumed file is "fresh", other not. 如果您已经使用git add file_name了修改过的文件,那么在git-checkout ,恢复的文件是“新的”,其他则不是。

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

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