0

So yesterday I made a push of my code to a Bitbucket repo. After realizing this was the incorrect repo to push to, I did a git reset --hard to remove the commit. However, after doing that, I noticed that my local files were deleted. Sadly, I did not create a backup of those recent files. Yes, I do have a much older copy of those files.

I've explored the .git folder and see that the commit prior to my git reset --hard is there. However, I am not sure how to get to the files within that commit. The .git folder has all of the appropriate folders: hooks, info, logs, objects, refs, as well as the main directory files (ie. HEAD, description, etc).

If I do a git fsck --full, I get a few errors:

error: HEAD: invalid reflog entry a5564cddeffd7673cf418955f01bec5d4003f630
error: HEAD: invalid reflog entry a5564cddeffd7673cf418955f01bec5d4003f630
error: refs/heads/master: invalid reflog entry a5564cddeffd7673cf418955f01bec5d4    003f630
error: refs/heads/master: invalid reflog entry a5564cddeffd7673cf418955f01bec5d4    003f630
dangling commit 141d684c3fd2d0da2d6df9844bfeea9e976c9b32

I looked in .git/logs/refs/heads/master to identify the commit I need.

The line with the one I want is this one:

452f1923ac02bfb1c0930ecfb39d7234db9b6f0f a5564cddeffd7673cf418955f01bec5d4003f630 [username] <email> 1594581184 -0500   commit: Initial commit

Is it even possible to get my files back? And if so, how can I do that? Thank you for any guidance you can provide! I normally back up my files, so this is uncharted territory for me.

sleepy_daze
  • 521
  • 2
  • 7
  • 24

2 Answers2

0

TLDR version of the linked answer :

git reflog
# copy the sha1 of the commit you want to return to
git reset --hard <sha1>
LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

Follow the below steps..

  1. To list all commits in the order you last referenced them (i.e. history of where your HEAD pointer travelled) you do git reflog

  2. Grab the hash of the commit just before the reset

  3. Go to that commit by git checkout <hash>

  4. Create a backup branch and switch to that branch git switch -c backup

  5. Go back to the old branch git checkout <branch_where_you_performed_reset>

  6. Merge the backup branch git merge backup

  7. Delete the backup branch

dgor
  • 704
  • 6
  • 19