Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore deleted file from repository

Tags:

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

like image 822
Eugen Avatar asked Jun 25 '12 22:06

Eugen


People also ask

Can git restore deleted files?

Recovering Deleted Files with the Command LineGit provides ways to recover a deleted file at any point in this life cycle of changes. If you have not staged the deletion yet, simply run `git restore <filename>` and the file will be restored from the index.

How do you undo a delete in git?

Even better, in cases like committing a file deletion and then wanting to revert it, Tower's awesome undo feature lets you get the file back by simply pressing CMD-Z!

How do I restore a deleted GitHub file?

In the top right corner of GitHub.com, click your profile photo, then click Your organizations. Next to the organization, click Settings. In the left sidebar, click Deleted repositories. Next to the repository you want to restore, click Restore.

Can you see deleted files in git history?

Listing all the deleted files in all of git history can be done by combining git log with --diff-filter . The log gives you lots of options to show different bits of information about the commit that happened at that point.


2 Answers

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.

like image 75
madth3 Avatar answered Oct 27 '22 08:10

madth3


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
like image 38
poke Avatar answered Oct 27 '22 06:10

poke