How can I revert a pull request locally?

Viewed 521

I have cloned a github repo using:

git clone

I then checked out a tag:

git checkout badCommit

Now, I have discovered a bug. The bug was introduced in a commit 2 months ago. I have the ID and all the info on this commit.

I want to:

  1. Remove this commit - only locally and test that my changes have the desired effect
  2. Create a new branch/tag with just this change
  3. Make an MR with these changes

What is the cleanest way to do this please?

1 Answers

Here is a very easy solution that does not remove the bad commit as you asked, but reverts its effects (variation which does really remove the bad commit at the bottom):

  1. Move HEAD back to your main development branch

You are currently in a detached HEAD state, so first you want to go back to the tip of your main branch (let's assume that it is master):

git checkout master
  1. Create a new branch and check it out
git checkout -b newBranch
  1. Revert the bad commit
git revert badCommit

This does not remove badCommit from the history, but it adds a new commit which removes the changes introduced by badCommit.

  1. Test

You can then test whether this fixes your problem.

  1. Submit MR

If you are happy, you can push newBranch to the remote to submit a MR. Depending on whether you have write access to the remote or not, this can look different, but something along the lines of (assuming the remote is called origin):

git push -u origin newBranch

If you really want to remove that commit from the history of your new branch, you can follow the same workflow, except that for 3. you would do:

  1. Remove badCommit from newBranch
git rebase -i badCommit~

(Note the ~: you are using the commit which precedes badCommit in this command).

Then replace pick by d or drop for the line corresponding to badCommit (it will be the top line) or delete that line and save.

You might have to resolve conflict(s) before being able to conclude the rebase if removing that commit creates any conflict.

If you go with this option, be very careful not to remove your bad commit from any of the branches that have already been pushed to the remote as it would create havoc for your collaborators.

Related