0

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?

Mr.UNOwen
  • 163
  • 1
  • 1
  • 14

2 Answers2

1

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.

Sinapse
  • 786
  • 1
  • 6
  • 23
  • Thanks, I will give this a test... I also did it another way. On a side note, if the hashes match up between github and local, am I in the clear? – Mr.UNOwen Sep 03 '17 at 10:35
  • This may have been a result of the prior attempt, but it just nuked the 2nd commit. – Mr.UNOwen Sep 03 '17 at 10:38
  • If the hash matches (with github) for the formerly mangled commit, am I in the clear to push my latest commit? – Mr.UNOwen Sep 03 '17 at 10:55
  • did you do the pull part, that is important to sync your local changes with the latest version of the repository. If you have the last commit of the repository(github) then you are safe to push your changes – Sinapse Sep 03 '17 at 10:59
0

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.

Mr.UNOwen
  • 163
  • 1
  • 1
  • 14
  • Can't ask another question, can someone tell me if the hash matches, am I in the clear to push the next commit? – Mr.UNOwen Sep 03 '17 at 10:51
  • You just did a very dangerous thing! My advise is that don't get used to this. There is always a clean way to manipulate your history with git commands. But for now I think you're good to go. – Sinapse Sep 03 '17 at 11:04