Restore Deleted Files from SourceTree or Git
Solution 1
If that files were committed you can just make git checkout HEAD -f
.
If files were not committed they are not stored as objects.
Solution 2
Here is a workaround in Sourcetree that worked for me:
-
Identify the commit where the file was deleted.
-
Go to Tools-> Options-> Custom Actions.
-
Create a new custom actions like this: custom action screen
checkout $SHA~1 -- $FILE
-
Go to the commit where the file was deleted (found in step 1), rick click on the file and select custom action-> your just defined custom action: Restoring the file
-
Unfortunately Sourcetree show an error and doesn't do the job. Error in Sourcetree GUI
-
I have the "Show Full Output" option always enabled so I have to close the window manually, this is useful when you need to create a merge request (the link will be there you'd just copy it, without this the window closes automatically and you'll miss the change to copy it). If you cannot see that window it's probably because you have that feature disabled (that's the default)
-
From the window copy the command.
-
Open a git terminal (from sourcetree top right) Git terminal launcher on Sourcetree
-
Paste the command (shift + insert) and press enter. Git Console
-
Voilá! your file is restored.
In the last image you can see (failed) attempts to restore from the commit the file was deleted, but you actually need to restore from the previous commit, that's the reason the first two attempts failed and the later succeeded (that's the reason for the ~1
at the end of the SHA).
Hope it helps, happy coding.
Comments
-
tomb almost 2 years
I have recently started using SourceTree to work with Bitbucket. I was working on a project, and then I decided I needed to move it to a different folder (for various reasons). So, I cut the files that I was working on in the repo, and then clicked the delete button in SourceTree, not realising that doing this would actually delete the files. I cut the files just in case, but didn't remember that pasting these files requires the original files to exist.
Now, I only have the files left from repo that SourceTree did not delete.. these are the files in the
objects
directory, and I do not have theinfo
orpack
folders that are inside theobjects
directory.Is there any way that I can restore the files? I have taken a look at this question and this question, however I am new to git and cannot figure out how to actually do these.
Please help! How can I restore the files in the repo?
-
hansvb about 11 yearsDid you commit these files before deleting them? Can you still see them in the timeline in an older revision?
-
tomb about 11 years@Thilo As I said, I deleted the whole repo from SourceTree. When I try to re-import it it comes up with the 128 error code. No I did not commit the files.
-
Joël Salamin over 9 yearsIf you never commit the deleted files, there is no way with git to restore them. Can you provide us the result of
git log --graph --oneline --decorate --all -20
? And the result ofgit status
will also be useful to better understand the situation.
-
-
DavidA almost 3 yearsJust tried this, and #1-4 worked perfectly in Sourcetree without any error. Perhaps they have fixed the issue that was previously causing it to fail. Thanks for the helpful answer!