1

I guess i messed up something with the git, although i don't get what exactly is happened :) So, initially, while being not in data directory (thought i was inproject's root), this is my command history:

 git add .
 git commit -m "v0.2.1"
 git push origin master
 git add .
 git commit -m "v0.2.1"
 git rm -r data
 git rm  data
 git rm data/file.txt
 git rm 
 git rm .
 git rm . -r
 cd ../
 git rm -r data
 git rm -r data/
 git rm -r .data
 git rm -r ./data
 git add .
 git commit -m "v0.2.1"
 git push origin master
 git reset --hard HEAD~1
 git push --force

i simply wanted to commit changes and then forget to remove the data directory from those changes. In the end lots of work in the main project directory lost - files are reverted to the state they've been at the previous commit (quite a lot was done since then). i guess it is happened because of git reset --hard HEAD~1 which probably was an advise from the error log since there were some problems. was in hurry and this is a result.

Any way i can restore a few local files to the state before this happened? tried solutions from here How to revert a "git rm -r ."? but nothing helps.

user1935987
  • 3,136
  • 9
  • 56
  • 108
  • Are the uncommitted changes made between `git push origin master` and `git reset --hard HEAD~1`? – ElpieKay Feb 28 '20 at 02:20
  • 1
    This is easy. Go to your reflog and find this commit: "git commit -m "v0.2.1". Now once you have found the sha simply do this: `git reset --hard SHA-you-have-found` and everything should be back to normal. – BenKoshy Feb 28 '20 at 02:21
  • oh thanks @BKSpurgeon , you saved me! :) – user1935987 Feb 28 '20 at 02:47

1 Answers1

1

This will get you your commited changes. But if you have uncommited changes, you basically cannot get them back, unless in very exceptional circumstances.

Steps:

1. Find original commit

Go to git reflog and find the original commit: "v0.2.1". I would recommend that you either use a git tag, or you give your commits different messages to allow for easy tracking.

2. Restore that commit

Once you have found it, use the sha to restore everything:

SHA-of-the-commit-you-want-to-restore (HEAD -> master) HEAD@{1234}: commit: v0.2.1 - the commit before everything was screwed up.

git reset --hard SHA-of-the-commit-you-want-to-restore

And everything should be back to normal!

BenKoshy
  • 33,477
  • 14
  • 111
  • 80