4

I know there are people having this asked already, but none provided a solution that helped me.

I use GitKraken, which obviously has no integrated functionality for checking out older commits in the history (I had to test a bit, no changes made). I now know that I should have used git checkout HEAD~n but instead used a soft reset. So GitKraken still shows that my changes are there and come after the one I reset to. But how can I get back to my latest commit? Checking out does not work either.

I'm not sure if GitKraken just displays it wrong, but Git tells me that the commit I reset to is the current HEAD. Seems correct.

Any way I can restore the following commits or set them as HEAD?

EDIT for the duplicate tag: The referenced thread is about going back the way it's intended (which I did not do) by checking out a previous commit. Since I did a soft reset the HEAD of my master branch was not the actual latest commit anymore but the one I reset my local repository to. Using git reflog with a reset helped to undo the actions done.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
SharpShade
  • 1,761
  • 2
  • 32
  • 45
  • Can you please explain what it is your trying to do? At the moment this is just a list of things you've tried with no context. – Liam Jan 28 '19 at 15:15
  • Get back to a commit which is newer than the current HEAD since I did a reset to it (there are 5 after it). But Git obviously does not realize that. When I reset to the newest commit, it seems to invert the actual changes done in those instead of restoring them... – SharpShade Jan 28 '19 at 15:17
  • Possible duplicate of [How to get back to most recent version in Git?](https://stackoverflow.com/questions/3559076/how-to-get-back-to-most-recent-version-in-git) – Liam Jan 28 '19 at 15:28
  • It probably solves the issue but from my point of view this would not have looked like it could serve my purpose without the risk of further damaging the repository. So I'd say my question is kinda different, though the solution might be the same. – SharpShade Jan 29 '19 at 09:03

1 Answers1

10

Get it from the branch's reflog.

git reflog [show] [log-options] [<ref>]

So in your case simply

git reflog branch-to-be-fixed

It'll output the list of previous operations on the branch

59a04ab96 branch-to-be-fixed@{1}: commit: ...message...

574c5ca23 branch-to-be-fixed@{2}: commit: ...message...

At this point, spot the commit you need in the output based on its message or hash, and use the handle to reset to the state you want :

git reset --hard branch-to-be-fixed@{1}
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 2
    `git checkout HEAD` would be fine. using `reset --hard` is using a steam hammer to open an egg – Liam Jan 28 '19 at 15:30
  • 3
    You do need to be careful with that. For example if you had a branch checked out, `reset --hard` will move that branch and potentially remove any commits that branch was pointing at. `reset` is really for moving branches and damned the consequences. It can get you into a lot of difficulty if your not careful. `checkout` is a **lot** safer – Liam Jan 28 '19 at 15:42
  • @Liam Yes but in this situation, the current branch to-be-repaired was implied as the context where `reflog` and `reset` are to be executed. And after a `git reset --soft` I don't understand how `git checkout HEAD` (since HEAD is now pointing at the wrong commit) would be of any use... what did I miss? – Romain Valeri Jan 28 '19 at 15:48
  • Should be `master` (or whatever). I don't use command line often. I'm just pointing out it can be very dangerous. [`reset` should be used with extreme caution, it can easily result in lost code.](https://stackoverflow.com/questions/3559076/how-to-get-back-to-most-recent-version-in-git#comment59401563_17996536) – Liam Jan 28 '19 at 16:03
  • 1
    I think RomainValeri is right, though. I tried to checkout HEAD but it did not do anything since the current HEAD was the commit I reset to. But I'll keep that in mind for the future - not swinging the reset so easily. – SharpShade Jan 29 '19 at 09:06