Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore deleted file with history in git

Tags:

git

github

How to restore an accidentally deleted file and folder in git after push. Below is the scenario.

  1. Accidentally deleted a folder which has a single file and made a bunch of changes to other files.
  2. Now, pushed all these changes. So this push has the deleted folder along with other changed files.
  3. On top of this, there were a few more pushes.

Now, can I restore the deleted file, along with history and then push back to repo.

like image 389
Venu Avatar asked Feb 17 '17 00:02

Venu


People also ask

Can we restore deleted file in GitHub?

If you want to restore a repository that was part of a fork network that is not currently empty, you can contact GitHub Support. It can take up to an hour after a repository is deleted before that repository is available for restoration. Restoring a repository will not restore release attachments or team permissions.

Is it still possible to restore deleted untracked files in git?

Once the untracked files are deleted, they cannot be restored. Before running the git clean command, perform dry run to know what the are files that will be deleted.

What happens to deleted files in git?

About file and directory deletionIf the file or directory you deleted contains sensitive data, the data will still be available in the repository's Git history. To completely remove the file from GitHub, you must remove the file from your repository's history.


1 Answers

Sure, just check it out from a commit where it existed. If the commit that deleted the file is the tip of whatever you have currently checked out, that’s HEAD^ (the commit before last):

git checkout HEAD^ -- path/to/file

Then you can commit and push it.

like image 143
Ry- Avatar answered Sep 23 '22 19:09

Ry-