Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deleted directory in Git by mistake

Tags:

git

By mistake I have deleted a directory in my git repo, and commited it.

I have done

git checkout LONG_SHA_ID

where LONG_SHA_ID is the ID of the previous commit, and I have got the directory back, but also I have got back the previous versions of some files that I have fixed in the latest commit.

How can I get back the deleted directory, with the latest version of the other files?

like image 332
Ferenc Deak Avatar asked Mar 25 '13 08:03

Ferenc Deak


People also ask

How do I restore a directory in git?

The command can also be used to restore the content in the index with --staged , or restore both the working tree and the index with --staged --worktree . By default, if --staged is given, the contents are restored from HEAD , otherwise from the index. Use --source to restore from a different commit.

How do I undo a git deletion?

If you're using the Tower Git client, you'll find the operations described above conveniently available in the GUI. Even better, in cases like committing a file deletion and then wanting to revert it, Tower's awesome undo feature lets you get the file back by simply pressing CMD-Z!

What happens if I deleted a .git folder?

The folder is created when the project is initialized (using git init ) or cloned (using git clone ). It is located at the root of the Git project repository. If we delete the . git folder, the information saved by Git will be lost, and the directory will no longer act as a Git repository.

Can you recover deleted git repository?

A deleted repository can be restored within 90 days, unless the repository was part of a fork network that is not currently empty. A fork network consists of a parent repository, the repository's forks, and forks of the repository's forks.


2 Answers

Do this:

git checkout LONG_SHA_ID -- /path/of/directory/you/deleted
like image 127
manojlds Avatar answered Oct 28 '22 19:10

manojlds


Checkout the branch with the fixed files, and do git reset --mixed HEAD^. This will undo the commit so you can try again without deleting the folder.

like image 42
Jhon Grotzke Avatar answered Oct 28 '22 18:10

Jhon Grotzke