Revert a merged pull request from master branch

12.1k Views Asked by At

I have accidentally merged pull request into master branch from my feature branch, which i should not have. I am using gogs . I cannot use revert button as there is not any functionality available. How can i revert back the master to the position before my pull request so that master branch will be clean again.

Also master branch is protected and commits should be pushed only through pull request. So git push -f is not working for me.

I have read other questions but they were about a specific incidents. I am not sure how can i achieve this.

2

There are 2 best solutions below

5
Antonio Petricca On

If you have not added any other commit after the wrong merge I suggest you tu rely on the command line as follow:

git checkout master
git pull

git log --oneline --graph --decorate # Write down the hash of the last good commit

git reset --hard {{last-good-commit-hash}}

# Review your commits, and if it all right...

git push --force # Rewrite branch history, be carefully!!!
4
leoleosuper On

I have had the same problem before. Going to the commit you want to revert to and hitting the revert button in Gogs is what you are supposed to do.

If that doesn't work: Every commit with Git has a commit hash connected to it. This should be listed in the history of commits on the Gogs page. There should be a button to easily copy any hash, so you don't have to type it out. If you can use a command line console, you can use "git revert [code]" where code is the commit hash linked to the earlier commit. After that, "git push".