248

I have 3 modified files (no new files) in a pull request at the moment.

I would like to remove one of those files from the pull request, so that the pull request only contains changes to two files and leaves the third in its original, untouched state.

I have tried a couple things (checking out the original version of the file, etc...) but it still shows as a changed file in the PR.

Is there a solution to this?

1
  • 1
    If the modifications to the file are in their own commit(s), you can do an interactive rebase and remove the commit(s) affecting the file you want unchanged, and then do a force push to your branch. Github should automatically detect that.
    – Dentych
    Sep 12, 2016 at 23:16

10 Answers 10

507

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

Reference:

See git checkout docs:

git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>…​

Overwrite the contents of the files that match the pathspec. When the <tree-ish> (most often a commit) is not given, overwrite working tree with the contents in the index. When the <tree-ish> is given, overwrite both the index and the working tree with the contents at the <tree-ish>.

Anything within [] is optional. In our case, we want to overwrite items identified by a specific pathspec with its respective content at a different branch/commit/tag

17
  • 10
    Not Working showing me the error: pathspec '{file/path.js}' did not match any file(s) known to git. Oct 2, 2017 at 10:06
  • 13
    It is working and proven solution just check your path correctly
    – Pavan T
    Jun 14, 2018 at 6:52
  • 2
    this works for the files around the file i actually want to perform the action on...so i dont think its something wrong with the path
    – Tim Boland
    Dec 18, 2018 at 2:25
  • 1
    In case those who have trouble with error: pathspec '{file/path.js}' did not match any file(s) known to git. I've got it when tried to reset file which didn't exist in origin/master branch Feb 21, 2020 at 13:13
  • 1
    @tim.rohrer it doesn't matter if you are an originator or a reviewer, steps are same. Oct 18, 2020 at 19:07
36

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
2
  • 1
    Nice simple answer. This should be the selected solution. Apr 26, 2022 at 18:28
  • 1
    Holy smokes! Did not know you could selectively checkout a file from a branch to revert the file (or set of fiels). Definitely will use this solution in the future! Jul 15, 2022 at 16:18
14

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:

  1. Close the PR so that whomever is reviewing it doesn't pull it in until you've made your changes.
  2. 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)
  3. Discard (or undo) any changes to the file that you didn't intend to update
  4. Make a new commit git commit -a -c ORIG_HEAD
  5. Force Push to your branch
  6. 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.

1
  • 1
    Step 6. Re-open pull request was the problem for me when using bitbucket. I could see some changes that I actually had removed. Re-opening the pull request solved the issue.
    – Shnigi
    Mar 12, 2021 at 10:07
1

EASY WAY for people who are new to git or using Azure DevOps.

If the file change is simple and the PR is still open, just go to your branch, modify the file back to the way it was originally and then commit and push your change. Your PR should be updated and the file will disappear if it exactly matches the target branch.

If its a complex change, do the same thing but you may want to look at the file history, copy the previous version (Ctrl-A on Windows), overwrite the version on your dev branch, then commit and push.

0
1

For those who prefer a UI solution over git commands... You can download the unmodified file from the master branch (or whatever branch you're merging into), then paste it into your local folder with the modified file, effectively replacing it with the unmodified version. Then commit the change and push it up.

1
  • 1
    It works and it is very useful when handling several files in a folder. Apr 28 at 16:13
0

For instance, you want to created PR from branch1 for files file1,file2 and file3. Now you want to remove file3 from PR.

  1. Checkout your branch from which PR was created. git checkout branch1

  2. Check commit history git log It will provide you with following details

    commit <some id 1> (origin/branch1, branch1) Author: abc Date: Tue Nov 2 16:47:03 2021 +0530

    commit <some id 2> (origin/branch1, branch1) Author: abc1 Date: Tue Nov 1 16:47:03 2021 +0530

  3. Look for commit id for commit made before your changes and checkout file3 with that commit id git checkout <some id 2> C:\Code\project\file3.java

  4. Then do git commit git add file3.java git commit -m "Removing file3 from PR" git push

  5. Check your PR, file3 should be removed now.

0

For those following along with the top answer, which is the correct one, and getting the error "error: pathspec" This can be fixed by running these commands in your repositories source folder

git rm -r --cached .
git add .
git commit -m "fixed pathspec error"

I would have added this as a comment but no reputation, sorry.

-3

Removing a file from pull request but not from your local repository.

  1. Go to your branch from where you created the request use the following commands

git checkout -- c:\temp..... next git checkout origin/master -- c:\temp... u replace origin/master with any other branch. Next git commit -m c:\temp..... Next git push origin

Note : no single quote or double quotes for the filepath

-5

A pull request is just that: a request to merge one branch into another.

Your pull request doesn't "contain" anything, it's just a marker saying "please merge this branch into that one".

The set of changes the PR shows in the web UI is just the changes between the target branch and your feature branch. To modify your pull request, you must modify your feature branch, probably with a force push to the feature branch.

In your case, you'll probably want to amend your commit. Not sure about your exact situation, but some combination of interactive rebase and add -p should sort you out.

-7

Just go to pull request in GitHub web and click more action (... 3 dots icon) on file > Delete file > Commit to your current branch

Delete File

Commit changes to branch

1
  • 1
    This delete the file. Does not restore to its previous version
    – Sequoya
    Mar 1 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.