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?

Was it helpful?

Solution

If that files were committed you can just make git checkout HEAD -f.
If files were not committed they are not stored as objects.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top