How to remove commits from a pull request
You have several techniques to do it.
This post - read the part about the revert will explain in details what we want to do and how to do it.
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.
People wouldn't like to see a wrong commit and a revert commit to undo changes of the wrong commit. This pollutes commit history.
Here is a simple way for removing the wrong commit instead of undoing changes with a revert commit.
git checkout my-pull-request-branch
git rebase -i HEAD~n
// wheren
is the number of last commits you want to include in interactive rebase.- Replace
pick
withdrop
for commits you want to discard. - Save and exit.
git push --force
This is what helped me:
Create a new branch with the existing one. Let's call the existing one
branch_old
and new asbranch_new
.Reset
branch_new
to a stable state, when you did not have any problem commit at all. For example, to put it at your local master's level do the following:git reset —hard master git push —force origin
cherry-pick
the commits frombranch_old
intobranch_new
git push
If you're removing a commit and don't want to keep its changes @ferit has a good solution.
If you want to add that commit to the current branch, but doesn't make sense to be part of the current pr, you can do the following instead:
- use
git rebase -i HEAD~n
- Swap the commit you want to remove to the bottom (most recent) position
- Save and exit
- use
git reset HEAD^ --soft
to uncommit the changes and get them back in a staged state. - use
git push --force
to update the remote branch without your removed commit.
Now you'll have removed the commit from your remote, but will still have the changes locally.