0

I had a lot of changes to commit and push so I committed them. I then tried to pull from GitHub but there were conflicts that couldn't be resolved so I used vimdiff to try and do it manually.

I remember using gitdiff a couple of times before and that it was really hard and unintuitive (even though vim is my editor of choice) but for some reason I was stupidly gung-ho and before I knew it I suddenly only had one screen, there were weird characters everywhere and it would only let me exit if I accepted the changes!

So I accepted and pulling again showed that it had merged. :(

There was a lot of additions in the commit and one debug cycle can take over 30 hours so it's essential I am able to undo the merge without losing the changes from the previous commit.

I'm sorry I know there are lots of posts about merging in git but most of them are related to merging branches which seems related but different and the few that are related to my pull situation I'm worried that I might loose the commits.

Can someone help me with this please? FYI, I'm on a Linux commandline.

ojunk
  • 879
  • 8
  • 21
  • So is all of this happening locally, to a _local_ branch on your machine? Is it also true that you have not yet pushed any of this work to the remote? – Tim Biegeleisen Sep 21 '18 at 10:54
  • @TimBiegeleisen well, I SSHed into a remote machine but all of it happened locally on that remote machine if you know what I mean? Nothing has been pushed to GitHub version. – ojunk Sep 21 '18 at 10:56
  • Even though your changes are merged you can easily retrieve them, unless and until the branch is deleted from remote as well as local. Can you share the graph you get after executing "git log --graph --oneline" and then share the commits(with id) you want and the error commits. – Rishabh Agarwal Sep 21 '18 at 10:57
  • And, one other question, _after_ the bad merge, have you committed any work which you want to retain? – Tim Biegeleisen Sep 21 '18 at 10:57
  • Thanks for the swift replies @TimBiegeleisen and rishab. I will have a look at the graph now. I did not commit anything after the bad merge but it is imperative that the commit before the bad merge is not lost. – ojunk Sep 21 '18 at 11:00
  • Possible duplicate of [Undo a Git merge that hasn't been pushed yet](https://stackoverflow.com/questions/2389361/undo-a-git-merge-that-hasnt-been-pushed-yet) – phd Sep 21 '18 at 11:39
  • https://stackoverflow.com/search?q=%5Bgit%5D+undo+git+merge – phd Sep 21 '18 at 11:39
  • @phd your example is a solution to merging a branch. I mentioned this in my original post. Thank you for the search link though, I didn't realise you could define tags like that. – ojunk Sep 21 '18 at 14:39
  • This is probably the duplicate though https://stackoverflow.com/questions/1223354/undo-git-pull-how-to-bring-repos-to-old-state Sorry must have got lost in the sea of branch merges. – ojunk Sep 21 '18 at 14:42

1 Answers1

0

Assuming you just need to undo a bad merge commit you made to a local branch, which has not yet been pushed/shared, you should be able to do just a do a hard reset. Find the SHA-1 hash of the commit to which you want to revert, and then do:

git reset --hard <SHA-1>

You can try running git log to find the most recent commit where you did your latest work. After this, presumably there would be one or more merge commits, which you don't want.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360