Search code examples
gitgithubpull-request

How to remove commits from a pull request


I did a pull request but after that I made some commits to the project locally which ended polluting my pull request, I tried to remove it but without any luck.

I found some similar questions on StackOverflow but I can't apply what's in there. It's my first pull request on GitHub so it's kinda strange to me how all of this works.

The highlighted commit is the one I need to keep and remove all the other stuff. It becomes the fourth commit in the history because I make some merge stuff.

enter image description here

my git log enter image description here

Can someone please explain what's going on and how to fix this problem?


Solution

  • You have several techniques to do it.

    I recommend of reading this answer:

    TL;DR; (from the recommended answer)

    enter image description here

    Here is the most simple solution to your problem:

    # Checkout the desired branch
    git checkout <branch>
    
    # Undo the desired commit
    git revert <commit>
    
    # Update the remote with the undo of the code
    # The force is a tricky flag since it will force the push but
    # your administrator can block it, so if it's not an option you
    # can delete the old branch and push it again
    git push origin <branch> --force
    

    The revert command will create a new commit with the undo of the original commit.