2

I'm looking for a way to remove an "double" commit I accidentally created on a repo. While attempting to collapse a branch's commits before merging it into the master, I unwittingly pushed the branch's changes onto the master (I know just enough Git to shoot myself in the foot). Later, when I merged the branch, I ended up with a second (merge) commit. The changes in this merge commit are exactly the same as those in the accidentally pushed commit. Now my master commit history looks something like this:

Some_Newer_Stuff_Here
The_Merge_Commit
The_Branch_Commit //Same contents, different Hash as commit above
Master_Commit_2
Master_Commit_1

I have attempted to use git interactive rebase (as detailed here and here), however when I do so, not all the commits appear in the list. Instead I see something like this:

Some_Newer_Stuff_Here
The_Branch_Commit
Master_Commit_2

The_Merge_Commit is missing (which I assume is the one I want to keep). Note that all commits do show up in git log. If I attempt to remove The_Branch_Commit using rebase it creates a bunch of merge conflicts.

I can't find any way to solve this problem, so any help would be appreciated.

  • possible duplicate of [fixing git double commit history](https://stackoverflow.com/questions/22817360/fixing-git-double-commit-history?noredirect=1&lq=1) – SovietFrontier Sep 23 '19 at 16:17
  • If you run `git show `, does anything show up? Also, if you could run `git log --oneline --graph --all -20` and paste the output here, it would really help! – Zach Olivare Sep 23 '19 at 19:09
  • You already have "Some_Newer_Stuff_Here", so you can't do anything about the history anymore because someone already made changes based on your merge commit. If you change the history and force-push, it will break things when other team members start pulling the changes. So forget about this, mistakes happen, move on. – Stanislav Bashkyrtsev Sep 23 '19 at 19:57
  • @Zach Posten `git show` does come up with a commit. `git log` is too many characters for a comment so I put it on [pastebin](https://pastebin.com/nPNtYNbH) For reference `The_Merge_Commit` is b5151cf and `The_Branch_Commit` is 84939ca. I'm sure the repo looks pretty bad, unfortunately I have basically no clue how to use Git correctly. – Niall Burton Sep 23 '19 at 20:04
  • Does your `git reflog` show a point in time when everything is hunky dorey? perhaps you can rewind to that point in time, then force push? – Michael Sep 23 '19 at 22:50
  • @NiallBurton I really want to help you get this figured out! Unfortunately, the pastebin you created doesn't contain commit b5151cf. That's my fault because I specified the `-20` in the command I posted above. Could you possibly create a new one that includes all the relevant commits? If you omit the `-20` from above, you can just copy the commits that are relevant – Zach Olivare Sep 24 '19 at 16:18

1 Answers1

0

Without more commit information, I'm not able to give you specific, step by step instructions. But I thought I could potentially answer one of the aspects of your question.

The reason that The_Merge_Commit doesn't show up in the interactive rebase is because the git rebase command by default ignores merge commits, instead preferring to lay commits one on top of the next.

You can change this behavior with the --preserve-merges flag however. Adding that to your git rebase -i command will cause The_Merge_Commit to show up in the editor page, and then you would be able to delete The_Branch_Commit. But there is still the potential for some annoying merge conflicts to happen.

git rebase -i --preserve-merges <HASH-OF-COMMIT-BEFORE-INCIDENT>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zach Olivare
  • 3,805
  • 3
  • 32
  • 45