3

In GIT working repo i removed couple of files with command rm -rf not git rm, When i try to pull/fetch from the server(bare repo) files are not visible. How do i bring those file back ?

maestromani
  • 841
  • 1
  • 9
  • 31

3 Answers3

4

Revert to a previous commit. This will reset to last committed state:

git reset --hard HEAD

Note...if you have other changes...be SURE you stash them first!

git stash

Here is an option taken from another post that might help you as well:

git checkout abcde file/to/restore

Where abcde is the commit #. You can use:

git log

To get commit log.

Reset or revert a specific file to a specific revision using Git?

Community
  • 1
  • 1
Calvin Froedge
  • 16,135
  • 16
  • 55
  • 61
  • 1
    if you can get the same effect without a hard reset, you should. it's a pretty dangerous command. – wilhelmtell Jun 20 '11 at 06:16
  • Thanks reset --hard HEAD brought back the file, Is reset --hard is dangerous, can you please explain more on that ? – maestromani Jun 20 '11 at 15:30
  • Yea. It reverts EVERYTHING under source control to the last committed state. That means that if you changed files a, b, c and d in a commit, but you only wanted to recover a (which you accidently deleted), using reset --hard HEAD would bring you back to the state of each of those files (all 4 of them) before you made your last commit. You're essentially "jumping back" one commit. – Calvin Froedge Jun 20 '11 at 18:47
3

I'd recommend git checkout path/to/file.ext if you only want to restore a single file (or do it a couple of times for a couple of files). Using git reset --hard HEAD will get all of your files back, but it will also wipe out any other changes you've made since your last commit that you might want to keep.

Amber
  • 507,862
  • 82
  • 626
  • 550
0

Always inspect the output of

git status

when in doubt. The output would have showed you how to get said filles back.

This would include instructions for deleted files and files/changes that got added to the index.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141