Remove a modified file from pull request
Solution 1:
Switch to the branch from which you created the pull request:
$ git checkout pull-request-branch
Overwrite the modified file(s) with the file in another branch, let's consider it's master:
git checkout origin/master -- src/main/java/HelloWorld.java
Commit and push it to the remote:
git commit -m "Removed a modified file from pull request"
git push origin pull-request-branch
Solution 2:
You would want to amend the commit and then do a force push which will update the branch with the PR.
Here's how I recommend you do this:
- Close the PR so that whomever is reviewing it doesn't pull it in until you've made your changes.
- Do a Soft reset to the commit before your unwanted change (if this is the last commit you can use
git reset --soft HEAD^
or if it's a different commit, you would want to replace 'HEAD^' with the commit id) - Discard (or undo) any changes to the file that you didn't intend to update
- Make a new commit
git commit -a -c ORIG_HEAD
- Force Push to your branch
- Re-Open Pull Request
The now that your branch has been updated, the Pull Request will include your changes.
Here's a link to Gits documentation where they have a pretty good example under Undo a commit and redo.
Solution 3:
Switch to that branch where you want to revert the file.
This is the command for it.
Just need to choose the remote and branch where your file would be restored to
git checkout <remote>/<branch> -- <file_path_from_project_root_folder>.
In my case, it was
git checkout origin/master -- .github/workflows/ci.yml
Solution 4:
Switch to the feature branch from which you created the pull request:
example : $ git checkout pull-request-branch
Overwrite the modified file(s) with the file in another branch:
$git checkout origin/master -- src/main/java/HelloWorld.java
Commit and push it to the remote:
$git commit -m "removed a modified file from PR"
$git push