244

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?

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
jlee
  • 2,835
  • 3
  • 16
  • 16
  • 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 '16 at 23:16

10 Answers10

504

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

mfaani
  • 33,269
  • 19
  • 164
  • 293
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • 10
    Not Working showing me the error: pathspec '{file/path.js}' did not match any file(s) known to git. – raftaar1191 Oct 02 '17 at 10:06
  • 13
    It is working and proven solution just check your path correctly – Pavan T Jun 14 '18 at 06: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 '18 at 02:25
  • With this method, you overwrite the file so you loose the work done on that file, right ? You can't keep it for another branch and/or another PR ? – Ernest Jones Mar 25 '19 at 10:54
  • @ErnestJones correct, if you have to keep the changes then you have to save it in a different branch. – Arpit Aggarwal Mar 25 '19 at 10:56
  • The solution with the soft reset is better for my case but thanks ! – Ernest Jones Mar 25 '19 at 10:57
  • 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 – Dmitry Malugin Feb 21 '20 at 13:13
  • THIS DOES NOT DO WHAT WAS ASKED. This adds yet another change to the git history of the file. This should not be marked as the correct answer. – doublejosh Mar 05 '20 at 19:14
  • Should this work if I'm the reviewer and not the person who originated the PR? If so, what should `pull-request-branch` look like? When I pulled it to change the files, I used: `git fetch origin pull/ID/head:name-of-branch`, but I when I tried to push to `ID:head/name...`, I got `error: src refspec ID/head does not match any`. So then I removed `ID:head/` and pushed. That created a new branch and did not update the PR. – tim.rohrer Oct 18 '20 at 18:28
  • 1
    @tim.rohrer it doesn't matter if you are an originator or a reviewer, steps are same. – Arpit Aggarwal Oct 18 '20 at 19:07
  • Thanks. So, what should be the format of the `pull-request-branch`? Prefixed with `ID:head` like is done when the PR is checked out? If so, that doesn't work. I suppose I could open a new issue but it seems we can improve this one. – tim.rohrer Oct 18 '20 at 19:13
  • No need of any prefix, simply checkout to branch from which pull request is created e.g `$ git checkout pull-request-branch`, do the required changes and push it to same branch. – Arpit Aggarwal Oct 19 '20 at 06:56
  • 1
    this is great! I created a new branch and checked out only the files i needed from my first origin/feature branch – Sonic Soul Jan 25 '21 at 04:22
  • 1
    This works great for changed files, but it won't undo added files. – Kalinda Pride Jul 22 '21 at 17:50
  • @DmitryMalugin so how do I restore a file to its original state (i.e. not existing) if it doesn't exist in the upstream branch? – Ben Alan Jun 21 '22 at 18:12
  • @BenAlan I don't remember exactly how I solved it I guess I simply deleted it – Dmitry Malugin Jun 28 '22 at 09:31
  • i thought copy pasting the code from main into my local file and committing it would remove the file from pull request since now what's committed is the same as what's in main, but it didn't :((((( ..... i did git checkout origin -- my_file.sql as you guys are saying, but now it's sayin "nothing to commit" and the file is still in the PR – Raksha Mar 10 '23 at 18:53
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
Umar Asghar
  • 3,808
  • 1
  • 36
  • 32
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.

Keif Kraken
  • 9,500
  • 1
  • 10
  • 12
  • 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 '21 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.

Bill
  • 2,382
  • 2
  • 24
  • 27
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.

SendETHToThisAddress
  • 2,756
  • 7
  • 29
  • 54
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.

Khyati Elhance
  • 662
  • 6
  • 11
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.

JNorm
  • 1
  • 1
-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.

Chris Kitching
  • 2,559
  • 23
  • 37
-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

Aviran
  • 15
  • 5