Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Remove unwanted commits from a Pull Request

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?

like image 842
user1152142 Avatar asked Jan 20 '12 10:01

user1152142


People also ask

How do I delete specific commits?

To drop a commit, simply replace the command 'pick' with 'drop' and close the editor. You can also delete the matching line. The following command will remove an entire commit e78d8b1 in one go using the --rebase-merges mode with the --onto option. That's all about deleting commits from a Git branch.

Can you remove pushed commits?

If you have a commit that has been pushed into the remote branch, you need to revert it. Reverting means undoing the changes by creating a new commit. If you added a line, this revert commit will remove the line.

How do I remove old commits from a branch?

Removing the last commit To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.


1 Answers

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.

like image 54
Simon Stender Boisen Avatar answered Oct 08 '22 18:10

Simon Stender Boisen