git reset --hard HEAD~
That just kicks you back to the commit just prior to your current (merge) commit. Are you talking about trying to undo a merge further back in the commit history?Say 'abc123' is the commit immediately before the merge commit. Run this:
git rebase -i abc123
In the editor window that appears, delete the commit(s) that you want to "pretend never happened". Exit the editor. Git then replays the commits from abc123 up to HEAD, except without the commits you deleted. Now your local branch is in a state that omits the premature merge commit. git reset --hard <previous commit's hash>
and afterward you may have to git push --force
...to overwrite the changes in the blessed/central/public/shared repo.A good solution like GitHub Enterprise or Stash will allow you to use pull requests, so you can require that code gets reviewed by a third-party before merging.
In the case of Atlassian Stash, you can set branch permissions, which allow only certain users to commit/merge to that branch, and you can also require that a merge request be reviewed by specific individuals before the system allows it to be merged.
Preventing the merge problem in the first place is a much better approach than trying to fix it afterwards.
In a perfect world, all problems would be prevented. Unfortunately, we live in the real world, where being able to fix it afterwards is very important.
if i merge two branches, push that to the remote and i wan't to undo it.
So actually reverting a merge in git isn't that painful. It's typically just git revert -m 1 sha_1_of_merge_commit
The problem is that if you then do more work on the branch and fix the problems that caused the merge to be reverted, redoing the merge doesn't work the way you expect; the commits that were previously merged don't get re-merged. So re-merging the branch consists of reverting the revert and then merging to get the new commits. Which is painful in-as-much as it requires you to know about the previous revert.
My favoured solution to this is to never merge, always rebase, which also has the property of giving a nice linear history that can actually be understood. However some people prefer a merge-based workflow (and if you happen to use github it is very difficult to enforce anything else, due to the Big Green Button), so it is a real problem.
I agree with your "always rebase" method, the readable history is well worth it alone.
This also gives you a chance to fix up any conflicts before merging, instead of possibly having to merge twice.