53

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?

alex
  • 479,566
  • 201
  • 878
  • 984
user1152142
  • 889
  • 3
  • 12
  • 18

2 Answers2

34

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.

Simon Stender Boisen
  • 3,413
  • 20
  • 23
  • Is force pushing only a problem if someone forks the branch my pull request was based on? Or if they do any fork at all from my repository? If they delete the offending branch can they sync with my repository again? Is there a way to remove commits from a pull request cleanly without force pushing? – endolith Jul 24 '13 at 19:59
  • 1
    @endolith Only if they base commits on your branch. Any commit that is a descendant of a commit that is later rebased will be affected and can't be trivially merged as the original commit no longer exists. As long as no one merges or base commits on your pull-request until your done force pushing your free to change your pull-request by force-pushing to your branch should you so wish. It shouldn't happen if you make it clear in your pull request that your actively changing it. – Simon Stender Boisen Aug 03 '13 at 20:33
  • The pull request has to be open for the change of branch to take effect – Bartlomiej Lewandowski Apr 29 '14 at 09:55
  • Just want to mention that this works really well with GitHub. Previously I used to close the pull request every time changes were requested and ended up creating more branches and pull requests. With this approach, it's like the unwanted commits were never there... and one gets to use the same branch and the same pull request. – Nagev Sep 13 '17 at 08:22
0

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.