Remove a modified file from pull request

GitGithubVersion Control

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 - Git

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.

Solution 3 - Git

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 - Git

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

Solution 5 - Git

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.

Solution 6 - Git

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 (origin/branch1, branch1) Author: abc Date: Tue Nov 2 16:47:03 2021 +0530

    commit (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 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.

Solution 7 - Git

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

Solution 8 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjleeView Question on Stackoverflow
Solution 1 - GitArpit AggarwalView Answer on Stackoverflow
Solution 2 - GitKeif KrakenView Answer on Stackoverflow
Solution 3 - GitUmar AsgharView Answer on Stackoverflow
Solution 4 - GitSiduView Answer on Stackoverflow
Solution 5 - GitBillView Answer on Stackoverflow
Solution 6 - GitKhyati ElhanceView Answer on Stackoverflow
Solution 7 - Gitmohammed hussamuddin hussamuddView Answer on Stackoverflow
Solution 8 - GitChris KitchingView Answer on Stackoverflow