jlee
jlee

Reputation: 3175

Remove a modified file from pull request

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?

Upvotes: 278

Views: 319118

Answers (10)

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29276

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

Upvotes: 567

JNorm
JNorm

Reputation: 31

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.

Upvotes: 3

SendETHToThisAddress
SendETHToThisAddress

Reputation: 3704

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.

Upvotes: 1

Aviran
Aviran

Reputation: 19

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

Upvotes: -10

Khyati Elhance
Khyati Elhance

Reputation: 748

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.

Upvotes: -2

Bill
Bill

Reputation: 2748

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.

Upvotes: 1

Umar Asghar
Umar Asghar

Reputation: 4044

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

Upvotes: 38

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

Upvotes: -4

Keif Kraken
Keif Kraken

Reputation: 10640

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.

Upvotes: 14

Chris Kitching
Chris Kitching

Reputation: 2655

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.

Upvotes: -5

Related Questions