Question

I need to undo a merge started in GitHub for Windows.

It's sync'd to a single repository and I tried to sync it but got a conflict error message so I made the mistake of "merge remote-tracking branch "origin/pp-chart" into "pp-chart".

I would like to undo whatever I have done, what should I do?

Was it helpful?

Solution

If it is pushed, my advice would be to stick to a revert from now on.

  • Git revert

In case you have a commit of your merge like this:

Merge branch 'pp-chart'

You can to a revert of the merge (and all it's commits) like this:

git revert -m 1 [hash of the merge commit]

If there is no merge commit (in case of fast-forward merge), you can revert them all but commit only at the end like this:

git revert --no-commit [first hash]
git revert --no-commit [second hash]
git revert --no-commit [third hash]
git commit -m "my message"

Using revert, you don't risk loosing anything as your commit history stays linear. You can always revert a revert as it is just a reverted diff of the original commit.

  • Git rebase

The other way would be to do a rebase interactive, and remove lines of the commits you don't want anymore, but then they will be lost, so copy your current branch as it for later recovery!

Then use the following command:

git rebase --interactive [hash of the commit prior to those you want to remove]

It'll open an editor in which you'll have to delete the entry of the commit you don't want. Use this last solution with care, but if you're not confident, maybe just stick with revert for a start.

  • Git reset hard

In case your faulty commit(s) just happened in your history, can can also got back in time using the following:

git reset --hard [the last good commit you want]

Then again, keep in mind your changes will be lost in the branch your are doing the reset!

OTHER TIPS

the good thing about git is that you can experiment quite easily, by just making a new copy of the repository. I expect you'll have to have to drop to the command line to do what you want though.

I'm no git expert, so may get shot down here, but if I was in your situation this is what I'd do:

  • create a new copy the the repository in a new directory git clone <repoUrl>
  • in the new copy look at the history to find the SHA hash of the commit that you want to be the new head (ie the one before your merge). I find this is easiest with the gitk GUI tool. gitk at the command prompt
  • once you have the SHA hash of the commit that you want to be the new head reset the local repo to be at that point git reset --hard <SHA of the commit>
  • check the local repo to make sure its what you want (ie open it in the ide, check in the file system and do a gitk and check that the history looks as you expect)
  • push to github forcing the local version over the top of the existing version git push origin -f
  • clone the github repo again somewhere else to check everything is ok. If it is delete the loacl repo that you don't want and off you go. If its not then you still have your (borked) original you can push over the top if you want.

This will make the github version as if you had never made your changes at all, but will lose data (you xcan still recover from a previous clone), so be sure that you are force pushing a version that is right.

might not be the best solution but I think this is what I would do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top