Remove modified files from a Git Pull Request but keep them with their changes on your local branch

The scenario described on the title of the article happens pretty often. You committed files, pushed them to a feature branch and created a new pull request to merge them to master. However, maybe you accidentally pushed modified files, for which work is still in progress and you do not want them on master yet. So now you want to remove specific files from the pull request but keep the changes you made in these files locally. How?

Step 1. Navigate to the feature branch

Chances are you are already in the feature branch where you changed the files that you want to remove from the pull request. If not then use the git checkout BRANCH_NAME command.

Step 2. Revert to the last or the last commits

git reset is the command that reverts the files of the branch to previous commits. In our case, we want a soft reset, since we want to keep the changes we done to the files locally. Forget about the hard option of the command for this scenario!

If you are using windows cmd, you will have to add the “” quotes around HEAD, like this git reset --soft "HEAD^". This command will revert to the previous commit. If you want to go further into the commit-history then use git reset --soft "HEAD~N" where N is the number of commits to revert.

Step 3. Commit again only the files you really want to merge to master

For that you will have to use the git add and then the git commit commands. Check my blog article about how to use these common commands.

Step 4. Push the new committed files to the existing pull request

For this step we are going to need the help of the git push origin YOUR_BRANCH --force command. Pay great attention to the force keyword. This will remove the files already being in the pull request and replace them with the files of the new commit we created on step 3.

Thats it. I hope this 4-step guide will help you in times of despair :)

comments powered by Disqus