If that files were committed you can just make git checkout HEAD -f
.
If files were not committed they are not stored as objects.
Restore Deleted Files from SourceTree or Git
-
11-04-2022 - |
Question
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 the info
or pack
folders that are inside the objects
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?
Solution
OTHER TIPS
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.