How do I restore git commit history

290 Views Asked by At

So I mangled my local commit history with by amending the last commit. FYI, I'm not experienced with GIT. I thought amend was used to undo a git add --all. So I ran the two commands in the Stack overflow question: How to undo "git commit --amend" done instead of "git commit"

However looking at the hash, I see it's still different. Without destroying the changes in source code, how do I reset the local git commit history to what I have in github?

2

There are 2 best solutions below

4
On

use reset --soft HEAD^1 then pull origin <branch>

It will take back the head by one commit without removing your local changes, then will pull your branch to update history. After that, save your changes as a new commit and push as you always do.

2
On

So unfortunately Sinapse's solution didn't work as I likely mangled the history more running the commands from that other stack overflow question.

In the end, I made another clone from github and replaced the .git folder with the one from the newly cloned one and then continued with the correct commit (haven't pushed the changes to github yet). The hash of the second commit (the one I screwed up) has a matching hash now.