-1

I mistakenly made one commit from my secondary GitHub account and then to delete that commit. I ran these two commands one by one:

git reset --hard HEAD~1 
git push -f origin development 

the commit didn't get deleted but some of my uncommitted changes from my project got deleted somehow. I'm not able to see those changes.

Anyone can help me how to get back those changes.

r121
  • 2,478
  • 8
  • 25
  • 44
  • If you had run `git reset --hard HEAD~1`, then the last local commit was for sure deleted. And about `reset --hard` please refer to [git reset](https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard). About getting those changes, the question will be, did you added them to stage area using `git add`? – kadewu Jul 04 '22 at 11:44
  • no, all those changes were unstaged – r121 Jul 04 '22 at 12:01
  • I rewrote the code for the part that got deleted – r121 Jul 04 '22 at 12:01
  • Does `git reflog` help you? It should show you the movements of HEAD, permitting you to get back from mistakes. – Filippo Jul 04 '22 at 13:11

2 Answers2

0

some of my uncommitted changes from my project got deleted somehow.

Not "somehow". You deleted all uncommitted changes. That, in part, is what reset --hard means.

I'm not able to see those changes. Anyone can help me how to get back those changes.

Probably not. Git records commits. Whatever is in a commit (committed) can be retrieved. But uncommitted is, by definition, not in any commit.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • but all of my uncomitted changes were not deleted only some of them – r121 Jul 04 '22 at 11:44
  • Yes, that always surprises me too. :) If you created new files, they are untracked and Git leaves them alone. But changes to previously existing files were overwritten by `reset --hard`. – matt Jul 04 '22 at 11:46
  • What you probably _should_ have done is `git stash` before resetting. But it's likely too late now. As kadewu implies, if you staged the changes with `git add`, you just _might_ be able to get them back. But otherwise there is really no chance now unless your IDE has been saving them for you (which is not a Git matter). – matt Jul 04 '22 at 11:49
  • I want to delete the last commit that I have made from my secondary account without affecting any of my local unstaged/uncommitted changes. Any idea how to do that? – r121 Jul 04 '22 at 12:00
  • That isn't what you asked. I suggest posing it as a separate question. You might need to give further detail, as I for one find the phrase "commit that I have made from my secondary account" incomprehensible. – matt Jul 04 '22 at 12:03
  • ok! will post a separate question. Thanks – r121 Jul 04 '22 at 12:03
  • just created one: https://stackoverflow.com/questions/72856459/how-to-delete-last-pushed-commit – r121 Jul 04 '22 at 12:06
0

Run git reflog show and choose the commit you want to go back. Then run git reset HEAD@{n}. In my case, it was reset HEAD@{2}.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Sue
  • 1
  • 3