ChatGPT解决这个技术问题 Extra ChatGPT

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?

git status will give you a hint about what command(s) to run
It sounds like you still have the old directory in your remote repo (and might even have it in your local repo, too). STRONG SUGGESTION: 1) Do a "pull" from your remote repo into a NEW repo (don't do any more damage to your local repo). 2) Try "checkout" ... or even "revert" in your new, local, repo: atlassian.com/git/tutorials/undoing-changes/git-revert. 3) Update the remote repo when you're sure everything is OK.
As with git-2.27.x and above, look here

N
Nick Volynkin

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

Also note Find and restore a deleted file in a Git repository

Files are deleted from working tree but not committed yet:

If you have not yet indexed (git add) your changes you can revert content of a directory:

git checkout -- path/to/folder

If the deletion is already indexed, you should reset that first:

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

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

git reset --hard HEAD

When files are deleted in some commit in the past:

Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.

git rev-list -n 1 HEAD -- <file_path>

Then checkout the version at the commit before, using the caret (^) symbol:

git checkout <deleting_commit>^ -- <file_path>

Restore the full working tree from a distant commit

git reset --hard <revision> 

git checkout -- path/to/folder/* does not work Note: the question is how to restore a folder not a file
@gman How can I retrieve all the files inside the directory means both the directory and its files. But the command surprizingly doesn't work for me too. I remember it did a year ago.
@gman but it works like git checkout -- path or git checkout -- 'path/*'. In your example you delete with git rm which the OP didn't seem to do. I've added instructions for such case. Thanks!
On Windows you need to put quotes like this: git checkout "<deleting_commit>^" -- <file_path>
It unstaged a bunch of stuff in the whole repo even though I specified a folder on the reset command. How does this make sense? When I ran the checkout it looks like it restored my folder. I have no idea what unstage did to the rest of my code, ah the joys of a peer to peer repository manager being used for centralized repos, sigh.
j
jBelanger

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


You use HEAD too. for example: git restore --source=HEAD~2 "Path_To_Restore". BTW it could a relative path.
I
Ivan Mushketyk

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

git checkout -- removed_directory does not work
git checkout -- removed_directory worked for me, but I've created this directory before (by checkout one of files in that directory). After creation all files were restored in folder by this command.
g
gman

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


i
iethree

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

git restore --staged some/deleted/folder

l
lostphilosopher

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.)


s
stldoug

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.


S
Shygar

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.


T
Tejas Veer

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

Go to File Clone repository Select Repository your Repo Click on History

https://i.stack.imgur.com/Mqu9M.png

Right Click on Commit that used to delete Folder/File

https://i.stack.imgur.com/30kdw.png

Select Revert changes in commit option Now Just Click on Push Origin

https://i.stack.imgur.com/rUlEP.png

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


j
justadev

for uncommited deletions, Its as simple as this :

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