Restore Deleted Files from SourceTree or Git
If that files were committed you can just make git checkout HEAD -f
.
If files were not committed they are not stored as objects.
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.