Eshi
Eshi

Reputation: 53

Undo Git --amend -> Delete commit

I have been searching for a solution to this mistake I did. Can't find it anywhere.

I was working on a larger change, and I did a commit --ammend to add this as a patch to my earlier smaller change. However, for some reason, I deleted the commit --ammend message in editor (which unfortunately deleted my commit as a whole). So I am now left without my changes, anywhere. Is there any way I can get my changes/files back?

ps I have learned my lesson not to keep the changes without committing them for very long. I would appreciate any help.

Upvotes: 0

Views: 311

Answers (1)

michas
michas

Reputation: 26555

Yes, not keeping uncommitted changes is indeed a valuable lesson. Before doing something important always look at your uncommitted changes and decide if you want to commit or discard them. (A well set up prompt will help you recognize if you workspace is dirty.)

Pretty much everything you ever committed is save and can be restored at any time. The tricky part is to identify the commit you want.

There are usually two ways to do so: git log and git reflog.

With git log you can list the official history of a branch. As long as you only commit your work once in a while that should be enough. If you however change your history by amending or rebasing you obviously will no longer able to see the abandoned version in the normal git log.

That is where git reflog comes in handy. This will record pretty much all actions you did together with the commit id which was the result of your action. Just type git reflog and you get a list of actions you should recognize. Time goes from bottom to top. Commit something and you get a new entry on top.

If you want to restore an old version just scan through the list for the wanted entry and use git reset to time-warp there. :)

Upvotes: 1

Related Questions