1

I mistakenly merged my git repo with one of its own remotes. I have tried to reverse this as outlined in this question but I only get the response:

$ git revert -m 1 <commit id> On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean

How do I fix this?

Community
  • 1
  • 1
grrrrrr
  • 1,395
  • 12
  • 29

1 Answers1

0

Read the whole answer prior to doing anything, preferably make a backup of the git repo (the whole project folder) if you feel unsafe using possibly destructive commands.

If you haven't pushed your changes after the bad merge:

If you type git reflog you should get a revision that marks your history just before the erroneous merge was made. If you don't care about any changes you may have done since that point in time (including locally committed/non committed files), I would use git reset --hard XYZ if XYZ is the reference that you liked while reviewing git reflog.

This will make your history look like nothing bad even took place, which I would prefer if I hadn't pushed any changes anywhere (because you don't really want to screw with other persons git history, in case they pulled from the remote that you pushed your 'bad' changes to).

If you have pushed your changes after the bad merge:

There are many hints in the official documentation located here: https://github.com/git/git/blob/master/Documentation/howto/revert-a-faulty-merge.txt (that page is linked to from man git revert).

I recommend that you skim through most of it to find some general reasoning and find the case that you want to try out. git revert preserves your current history and builds onto it, contrary to git reset which let's you backtrack your history and 'erase' parts of it. If you made a backup of your git repository, you should be able to play around and learn a thing or two while at it :)

chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • So I have taken the steps to revert the commit, the notes saying: This reverts commit , reversing changes made to . Is this the best that can be done? My repo still shows 2x the number of actual commits and the network graph shows two basically identical branches in paralell – grrrrrr Jun 03 '16 at 14:24
  • It's common practice/courtesy to not rewrite the history that's pushed. That leaves one option - push changes that levels off the previous changes. How that looks in the git history depends a bit on if you did a fast forward merge or not, if you squashed commits prior to merging, etc. You shouldn't be afraid to let the git log show lots of commits, they are just pointers to git objects so they should take up that much space. I would assume squashing the revert of a non-ff merge into a separate "single commit revert" including all diffs, would be harder to backtrack. – chelmertz Jun 03 '16 at 14:40
  • so basically the history will just look at bit ugly due to this mistake but that's not a big deal? – grrrrrr Jun 03 '16 at 14:47
  • If you already pushed it, you can clone your branch and look at it yourself. There should be no difference in the files' contents, but that you already knew. If you haven't pushed it to "origin/master" already (assuming a "standard" naming standard) I would not use a revert, I would use the `git reset --hard` approach. The history being ugly is probably not a big deal, you can decide between that and forcing users that `git fetch`-ed (or `git pull`-ed) from you to `git fetch` from you and then `git reset --hard` to your newly pushed history, that's the options that I've used this far at least. – chelmertz Jun 03 '16 at 15:00
  • So I have already pushed to origin/master, does your second recommendation to have users ```git reset --hard``` still apply? It is only myself and one other user... – grrrrrr Jun 03 '16 at 15:17