简体   繁体   中英

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)

I tried git pull upstream master and git fetch upstream master to get those files and folders back to my local repository.

What I did is basically like,

  1. I got a repository from git and placed it locally
  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)
  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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