Restore Deleted Files from SourceTree or Git

16,053

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:

  1. Identify the commit where the file was deleted.

  2. Go to Tools-> Options-> Custom Actions.

  3. Create a new custom actions like this: custom action screen

    checkout $SHA~1 -- $FILE

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

  5. Unfortunately Sourcetree show an error and doesn't do the job. Error in Sourcetree GUI

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

  7. From the window copy the command.

  8. Open a git terminal (from sourcetree top right) Git terminal launcher on Sourcetree

  9. Paste the command (shift + insert) and press enter. Git Console

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

Share:
16,053
tomb
Author by

tomb

Visual stuff maker/programmer, demoscener, lurker. He/him.

Updated on July 20, 2022

Comments

  • tomb
    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 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?

    • hansvb
      hansvb about 11 years
      Did you commit these files before deleting them? Can you still see them in the timeline in an older revision?
    • tomb
      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
      Joël Salamin over 9 years
      If 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 of git status will also be useful to better understand the situation.
  • DavidA
    DavidA almost 3 years
    Just 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!