Ulysses
Ulysses

Reputation: 6015

Remove a file from a Git Pull Request

I have added a local changes to an existing file into my PR by mistake. I want to revert only this file without deleting this file locally. I just want that it doesn't show up in the Pull Request. I don't want to create new branch as many people have commented on other files in this PR.

Upvotes: 79

Views: 209809

Answers (16)

Eugene Yarmash
Eugene Yarmash

Reputation: 149786

You could 'unstage' a particular file with git reset and then amend the original commit:

git reset HEAD^ path/to/file
git commit --amend --no-edit
git push -f

This will not affect the version of the file in your working copy.

Alternatively, if you want to remove multiple files, you can reset the branch's head to the previous commit and create a new one:

git reset HEAD^  # move the tip of the branch to the previous commit
git commit -C ORIG_HEAD file1 file2 [...]  # list the required files
git push -f

Upvotes: 53

biplab rout
biplab rout

Reputation: 294

This does the trick -

Switch to the branch where you need to change:

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

Upvotes: 1

Max Alexander Hanna
Max Alexander Hanna

Reputation: 3863

Alternate method, more tedious, but without using a git command:

Go to the diff comparison between the files, just copy the old file and paste it in your repository as it was before, then reupload. it will automatically remove it from the PR because the files are now matching again.

this is equivalent to the git command : git checkout -- the_filename

Upvotes: 2

Siddy Hacks
Siddy Hacks

Reputation: 2310

enter image description here

Just click Delete File and you should be able to follow next steps easily

Upvotes: 2

justdvl
justdvl

Reputation: 874

For me this command worked:

git rm -r --cached pathname/filename

Upvotes: 4

Jared Wilber
Jared Wilber

Reputation: 6795

If you updated a file that already existed and want to remove it from the PR:

Assume you're working on a branch off of staging, and you PR'd a file named validate_model.py.

To remove this file from the PR and revert its changes, simply do:

git checkout staging -- validate_model.py

Upvotes: 108

Md. Shahariar Hossen
Md. Shahariar Hossen

Reputation: 1715

I have an easy solution:

  • Remove the git memory: git rm -r --cached .
  • Add: git add --all
  • Commit: git commit -m "<COMMIT_MESSAGE>"
  • Push: git push origin <BRANCH_NAME>

Upvotes: 2

Tunbola Ogunwande
Tunbola Ogunwande

Reputation: 133

git checkout -- the_filename

This will discard previous changes to the file and revert it to the last committed change.

You can learn more here Git-Basics-Undoing-Things

Read the section Unmodifying a Modified File.

Upvotes: 9

Vishwa G
Vishwa G

Reputation: 603

To remove the file from pull request which was added new follow below steps from your branch,

git reset HEAD^ path of the file

then do amend commit, then do force push.

To remove the file from pull request which was existing(just modified in your latest PR) follow as below,

Open the file in any text editor (like Notepad or npp). Replace (cut and paste) the contents of the file with unmodified version (likely copy from the source latest updated or simply not having your changes). Then stage and amend commit the file. Then do a force push.

Upvotes: 4

Vasantha Ganesh
Vasantha Ganesh

Reputation: 5060

  • Lets call your PR is from feature-branch to master.
  • Make sure you are in your feature-branch.
  • Now lets say, you've made some unnecessary changes to ./abc1.py and you want your abc1.py to be the same as master.

Run the following command:

git checkout origin/master -- ./abc1.py

Now if you run:

git status

You'll have the same changes as your master branch. Now you can commit your changes and push.

Upvotes: 42

Shrey
Shrey

Reputation: 21

Go to your remote/origin branch (not master) where you have pushed your changes (from where you have created a pull request). Delete that specific file from there. In your local, stash your changes, then pull all the changes from remote branch which will remove that file and then pop your stash to get all your changes back along with deleted files.

Here are the steps:

  1. delete file from your remote/origin branch and commit (won't be possible without it)
  2. git stash
  3. git pull
  4. git stash pop

Upvotes: 2

Maddy
Maddy

Reputation: 521

You can also use Source tree app and reverse the hunk. If no file change was there then it would be removed from the PR.

Upvotes: 1

Mr. Blond
Mr. Blond

Reputation: 1167

There are probably many ways how to remove/undo file changes in your PR.
I didn't find exact steps how to do this, so here it is:

Scenario:

Lets suppose that the file that you want to remove from PR is:
C:\GitRepos\MyProject\SomeFile.cs

This file is either modified or contains no changes (is the same as in master) but still is shown in your PR.

Solution:

1) Make sure that you have checked out the feature branch from which the PR has been created.

2) Search the file commit history: git log "C:\GitRepos\MyProject\SomeFile.cs"

You will see multiple records in format:

   commit <commit id> 
   Author: <author name> 
   Date: <Date of the commit> 
   <commit message>

3) Find the commit before you made any changes to the SomeFile.cs. Copy the commit id.

4) Check out that commit using previously copied commit id:

git checkout <commit id> "C:\GitRepos\MyProject\SomeFile.cs"

5) If you now check the git status then you will see that the SomeFile.cs is now under pending changes.

6) The last thing that you need to do is to commit these changes by simply using:

git commit -m "Remove the SomeFile.cs from PR"

And push your changes. :)

Upvotes: 11

1antares1
1antares1

Reputation: 1214

In my particular case, they just didn't want to be reflected as: File changes (regardless of the commits to be added).

I just deleted the files and performed Commit/Push.

When updating the Pull Request, they are no longer reflected (as if they were never added).

I hope this can be useful.

Upvotes: 1

SauerTrout
SauerTrout

Reputation: 499

You will need to know the hash value from the most recent commit effecting the file you want to remove from your Pull Request, hereafter , and the path to the file to which you want to undo all changes. Then, execute these commands from your working directory:

1) git checkout <my_feature_branch>
2) git checkout <latest_commit_hash> -- <Path_to_file_you_want_to_remove>

Example:

git checkout experimentBranch
git checkout xxxx123 -- MyProject/Resources/Constants.java

With these changes, the file in question will revert to how it was at the start of your branch, i.e., before the first commit on your feature branch.

Upvotes: 2

AnoE
AnoE

Reputation: 8345

I assume what you mean is that you committed a change to some file X and other files Y, Z and so on. Now you want to undo only the change on X. You don't want to show it in the push (I assume you mean push instead of pull), so you cannot simply edit X back to how it was before (maybe you accidently committed a password or other secret in your source file or whatever...).

If there is one single commit which introduced your change in X, then it will be easiest if you use git rebase to "skip" that commit. Let's say previouscommit is the commit just before your change, and brokencommit is the commit you want to remove, then:

git rebase --onto previouscommit brokencommit yourbranch

If, on the other hand, the changes on X are mixed across many commits and/or combined with changes on other files, then (where previouscommit is the last "good" commit):

git checkourbranch
git -i rebase previouscommit

In the interactive rebase, chose the edit action for every line where X was changed, and edit it out manually. c/f git help rebase for more details, though it should be pretty obvious what to do.

Upvotes: 3

Related Questions