I want to bring back COMMITED but then deleted files on my local repository after doing git reset hard

59 Views Asked by At

I have staged and commited a very large folder (created and only present on my local repository) but then I did git reset hard to revert my big commit which was a mistake. However, luckily, I commited my folder before reseting it! I can track lost files (the hash ID) with git reflog. Now, I am unsure what would be the correct action next. I found online many advice but again many suggest hard reset... I would like to know if it is possible to bring back the deleted folder to my local repository. I want to merge it with my master. I did some other commits afterwards but I have them saved on both remote and local repository. My only concern now is that I am missing this one folder which is apparently deleted but luckily was commited so I hope there is a way to bring it back to my local repository and re-do the commiting but in smaller batches because the one single commit is to large to be pushed to gitlab.

I tried git reflog <hash-ID> after being suggested here: https://www.freecodecamp.org/news/how-to-recover-a-deleted-file-in-git/ as a solution but I do not see any changes with git status or git checkout.

However, after I typed into git bash git checkout <hash-ID> I got this message:

$ git checkout 2f733ea
Updating files: 100% (1141/1141), done.
Note: switching to '2f733ea'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 2f733ea adding files needed for generating sampling points and maps

How do I save the lost/deleted files from the detached head back to my master?

1

There are 1 best solutions below

0
Kafkaese On

Well the message you got tells you what to do. Do git switch -c <new-branch-name>. This will create and checkout to a new branch that has the current commit at its tip.

If the hash-ID was the correct one, your new branch should now contain your 'big commit' and you can merge this into your master or whatever else you may want to do.