Search code examples
gitmergegithubresetrevert

reverting push'd git commit


I've got a repo with two branches-- master and dev. I was working on the master branch and pulled, and got a message that the repo was up to date. I committed my changes, and pushed to the remote repo (on github). I got a message saying that some changes were rejected.

I then did a git pull origin dev, which apparently was the wrong thing to do-- since it merged the dev branch with my master, and like an idiot I didn't notice this until I'd already pushed again. So the last commit shows Merge branch 'dev' of github.com:myuser/myrepo.

I can get back to the last known good status on my local repo by doing a git reset --hard [sha], with [sha] being the commit before the merge (though I'm not sure how to then make that change to the origin)-- or from what I've read I can also do a git revert -m and then commit/push that change.

Can anyone walk me through the "right way" to undo my merge, and restore both branches back to where they were prior to the merge?

Thanks-- if it matters this is a shared repo with only two developers, so it's not under heavy changes.

Edit to add: please talk to me as if I were a child. I have to admit this Git stuff still confuses me, so I'm far from a power user! Thanks


Solution

  • The git reset --hard [sha] will correct the branch on your local repository. To force this push to work, you can do a git push origin +master:master. The + sign will force the non-linear push to work.

    If the other developers have already pulled you wrong commit, they will have to do a git remote update, then a git reset --hard origin/master (provided that they are on their master branch, and have not made any other commits.

    Please use these commands with some caution :-). Good luck.