Home > Software design >  Recover deleted files from local git repo?
Recover deleted files from local git repo?

Time:05-20

I accidentally rm'd some files in a git repo. Those files had been changed a lot and hadn't been git add'd yet.

Is there a way to restore the files to their 'last saved' state?

CodePudding user response:

You can't do that with git.

git can bring your files back with their last commit state rather than last saved state.

If you use vim, maybe some *~ backup files are still in your git repo, then you can rename it.

If not, try some recovery tools like TestDisk.

BTW: rm is a dangerous command, try to add -i option with it: rm -i xx.

CodePudding user response:

Check first your IDE features.

You might be able to restore your changed files that way.

CodePudding user response:

As @tkausl says you can recover git added files that aren't in a commit yet with a little more effort and awareness of how git stores added content, but git add is how you tell Git about new content, lots of times I'll git add -p to add stuff I've smoketested and continue with stuff I'm still beating on.

So, no,not unless your editor's got undo history going back that far.

CodePudding user response:

If you deleted multiple files locally and did not commit the changes, go to your local repository path, open the git shell and type.

$ git checkout HEAD .

All the deleted files before the last commit will be recovered.

Adding "." will recover all the deleted the files in the current repository, to their respective paths.

For more details checkout the documentation.

  • Related