20

I'm being in a transition from SVN to GIT and got a question for which I cannot find an answer. I'll describe am usual scenario when I work with some open source projects via SVN.

  1. make a checkout
  2. start messing around with files, make changes, to test how the project works.
  3. after p.2 some files are heavily modified and I have no chance to get back to original state.
  4. I delete the modified files from disk "rm filename.cpp", run the command "svn update" and voila, all original files are back.

All this works fine with GIT as well, except p.4. I try to make "git pull" it says that project is up to date and I don't get the original files even though they are missing from local folder.

What is the correct command for p.4 when working with git. Thx

Eugen
  • 2,934
  • 2
  • 26
  • 47
  • 1
    possible duplicate of [How do I restore files to previous states in git?](http://stackoverflow.com/questions/35284/how-do-i-restore-files-to-previous-states-in-git) – Saheel Godhane Jan 17 '14 at 00:49

4 Answers4

19

The simplest way is indicated in the output of git status:

git checkout -- file

where file can be the name of a deleted file. This will recover the deleted file without affecting other files.

madth3
  • 7,275
  • 12
  • 50
  • 74
  • well, but what to do if there are 10 files I want to restore and I don't remember their names? – Eugen Jun 26 '12 at 00:01
  • `git status` will tell you the names of every changed/deleted file – madth3 Jun 26 '12 at 00:17
  • 1
    Also if you have deleted a folder lets say /src/test, and suppose it has many files under it even then you can restore them by simply running the following command: git checkout -- src/test – user204069 Feb 17 '17 at 21:00
17

Try to checkout the current branch (or HEAD):

git checkout HEAD

Or if you want to revert everything to the last commited state (warning: this will permanently delete all uncommited changes!), you can also reset hard:

git reset --hard
poke
  • 369,085
  • 72
  • 557
  • 602
  • Neither of this is working. I delete some files, after I do checkout it says that some files have D flag, nothing else happens. If I do reset, it says there is apull request which I don't need and again nothing else happens. If I make pull it says the files are up to date, but the deleted files are still missing. – Eugen Jun 26 '12 at 00:07
  • Thanks friend, your help saved my day! – Pawan Parashar Apr 22 '20 at 05:56
1

This is what I would do:

git reset HEAD <file-path> git checkout -- <file-path>

valenz
  • 311
  • 1
  • 2
  • 13
0

Git is awful in this case, and I have been struggling with it so much that it is incredible. Here is the way I do it:

  1. Clone the remote repo to some place on your local HD different from where you have your existing local repo
  2. Copy the deleted files/directories from the newly cloned repo to the existing local repo
  3. Commit the changes to your existing local repo, if needed push also to the remote repo
  4. Delete the newly cloned local repo

Why it should be this complicated and time consuming to get such a simple task done escapes me...

OppfinnarJocke
  • 1,038
  • 2
  • 9
  • 21
  • now, git zen: think you have only local repo and no remotes. And you wish to retrieve file... – ljgww Apr 20 '18 at 13:14