Git Remove unwanted commits from a Pull Request

GitGithubPull Request

Git Problem Overview


I have started working on a project and I made some unwanted commits which I pushed to origin master. Now, when I try to do a pull request, Github wants to commit all of the previous commits.

My question is, how do I remove the unwanted commits and commit the changes that I want to commit so that I am up to date with master?

Git Solutions


Solution 1 - Git

I assume your origin is your own fork of some project that you want to do a pull request too?

Since you will be changing history (by resetting the head) you will need to push with --force flag. Locate the hash of your last good commit using git log.

Now run

git reset SHA

This will change your head to that sha and preserve the changes in the files since that last good commit, your index will also be reset.

Now you can change your code and do the commits you want. But you have to do git push --force since you changed the history of the repository. This means that anyone who forked your repository won't be able to pull changes from you anymore. But you will be able to do a pull request to your upstream.

Solution 2 - Git

If you are using git gui, Goto git gui and visualize your branch history. (before doing the next step, take a backup of the local changes that you want to push) Right click to the point where the branch wants to reset to master and click reset. After resetting, in command line, type git push -f Now make the necessary changes in the branch , commit n push again. If you create a pull request now, it will have only the new commit after the branch reset.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1152142View Question on Stackoverflow
Solution 1 - GitSimon Stender BoisenView Answer on Stackoverflow
Solution 2 - GitvanithaView Answer on Stackoverflow