json2021
json2021

Reputation: 2307

Restore a deleted folder in a Git repo

I have deleted all the contents inside a folder and the folder is empty. I still had a copy in my remote repo. But when I did a git pull it didn't put back the deleted files isn't is supposed to do that?

So I did some research and saw that you can revert a file by doing git checkout <revision> -- <name of file>

But that only works on files.

How can I retrieve all the files inside the directory?

Upvotes: 161

Views: 179692

Answers (12)

Nick Volynkin
Nick Volynkin

Reputation: 15079

Folder was deleted, but not committed yet

Everything you can do with a file, you can do with a folder too.

If you've just recently removed the folder but have not yet indexed (git add) the changes, you can undo the changes with one command:

git checkout -- path/to/folder

If you have already done git add, you should first reset it, and then restore the contents:

git reset -- path/to/folder
git checkout -- path/to/folder

Restore the full working tree (not a single folder), but lose all uncommitted changes

If you've removed the contents of several folders, or made other unwanted changes, you can undo them all at once:

git checkout --

Folder was deleted and the change was committed some time ago

First, find the latest commit that had affected the given path:

git rev-list -n 1 HEAD -- path/to/folder

This command will return the hash of the commit that deleted the file.

Next, checkout the file from a commit before this one, referring to it using the caret (^) character:

git checkout <deleting_commit>^ -- path/to/folder

Also, see How do I find and restore a deleted file in a Git repository?

Upvotes: 380

iethree
iethree

Reputation: 91

As of git 2.24.0, there's a new git command: git restore:

git restore --staged some/deleted/folder

Upvotes: 8

Fritz
Fritz

Reputation: 411

You can restore a folder by using Git commands.

First, run the command:

git status

This will let you see the root folder and all of the files.

Example, let's say you deleted a root folder called lib

Files:
  lib/commands/123
  lib/commands/123/456
  lib/commands/123/456/789/etc

Restore it by using this git command:

git restore *lib

Upvotes: 0

sai
sai

Reputation: 1

If you did some changes but they have not been committed yet, then do:

git status

The output will show the commands for restoring files:

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)        
  (use "git restore <file>..." to discard changes in working directory)

For a single file,

git restore env/dev/mycode.py

For an entire folder, which has many other file where you want to restore,

git restore env/dev/

Upvotes: 0

Tejas Veer
Tejas Veer

Reputation: 533

If you have to Recover Without Git Command Then Follow This

If You have Github Desktop It will make things easy

If not then install and sign in

  1. Go to File

  2. Clone repository

  3. Select Repository your Repo

  4. Click on History

enter image description here

  1. Right Click on Commit that used to delete Folder/File

enter image description here

  1. Select Revert changes in commit option

  2. Now Just Click on Push Origin

enter image description here

  1. All set! You Have recovered the deleted File/Folder you can check it in your Repository

Upvotes: 0

Shygar
Shygar

Reputation: 1252

For me I temporarily deleted a folder due to an unrelated issue. Instead of trying to restore it from a backup, I just ran git restore folder/ and it restored it to what the branch had originally.

Upvotes: 10

stldoug
stldoug

Reputation: 850

If you are just looking to recover a deleted folder and you have other commits after the deletion, then you can also just goto your project on github.com.

From github.com, go you to your last commit that has your folder. You should see the commit message and to the right there's a button labeled "Browse Files". Clicking this will take you to all the files from that stage of the commit.

From there you can clone the code or just download the code as a zip.

Upvotes: 0

jBelanger
jBelanger

Reputation: 1778

You can restore files or folder with git restore.

git restore --source master~1 "PATH_IN_YOUR_REPO"

Here, master~1 reverts your folder to "1" revision back from your master branch.

Source: https://git-scm.com/docs/git-restore

Upvotes: 22

user128511
user128511

Reputation:

The only thing that worked for me was to checkout the repo in another folder. Assume the current repo is in /home/me/current.

I then did

git clone /home/me/current /home/me/temp

This make a separate clone of the repo in /home/me/temp

I can now go to /home/me/temp and do whatever I want. For example

git reset --hard commit-hash-before-delete

Now I can copy the deleted file folder back

cp -r /home/me/temp/some/deleted/folder /home/me/current/some/deleted/folder

And delete the temp folder

rm -rf /home/me/temp

The examples of

git checkout -- some/deleted/folder
git checkout -- some/deleted/folder/*

DO NOT WORK

$ git checkout -- some/deleted/folder/*
zsh: no matches found: some/deleted/folder/*
$ git checkout -- some/deleted/folder
error: pathspec 'some/deleted/folder' did not match any file(s) known to git.

Other examples like

git reset --hard HEAD

are destructive beyond just the deleted files. Any other changes will also be lost.

Similarly

git reset --hard some-commit

will lose any commits after some-commit

Upvotes: 7

justadev
justadev

Reputation: 1047

for uncommited deletions, Its as simple as this :

git reset HEAD rel/path/to/deleted/directory/*

Upvotes: -2

Ivan Mushketyk
Ivan Mushketyk

Reputation: 8285

If you have not yet commited your changes you can revert content or a directory:

git checkout -- removed_directory

If you want to revert all changes do:

git reset --hard HEAD

Upvotes: 12

lostphilosopher
lostphilosopher

Reputation: 4501

If you don't specify a specific file you should be able to pull the full contents of a specific commit. Like: git checkout 264794319e9695ba843cd6 (assuming that hash has all your files at the right state).

The reason pull isn't restoring files is that git sees your deletions as the more recent change, applying that on top of whatever you're pulling.

(I'd recommend experimenting in a new branch.)

Upvotes: 0

Related Questions