How to remove a file from Git Pull Request
Asked Answered
S

8

18

I have a pull request opened where I have some project.lock.json files which I do not want to merge while merging my branch to main branch. Is there a way to remove thos project.lock.json files from my Pull Request?

Scenography answered 21/4, 2016 at 7:55 Comment(1)
There is no such thing as pull request in git. pull requests are a github thing, not a git thingBruell
F
2

You need to remove file, commit changes and make next push to your branch.

If you want leave file in your branch, but not to merge it to main branch, you can delete it in one commit, then add again in another. Git allows you manually accept certain commits using git-cherry-pick. You can accept each commit except that in which you have added this file again.

Fitter answered 21/4, 2016 at 8:4 Comment(0)
F
17

Please do let me know if there is a better way of doing this. This is the workaround I have found.

list remote branches

git branch -va

checkout the PR branch

git checkout origin pr_branch

overwrite pr_branch's file with other_branch's file

git checkout other_branch -- ./path/to/file

commit changes

git commit -m "overwrite with other_branch's"

push your changes

git push origin pr_branch
Fineable answered 23/2, 2017 at 2:24 Comment(0)
F
2

You need to remove file, commit changes and make next push to your branch.

If you want leave file in your branch, but not to merge it to main branch, you can delete it in one commit, then add again in another. Git allows you manually accept certain commits using git-cherry-pick. You can accept each commit except that in which you have added this file again.

Fitter answered 21/4, 2016 at 8:4 Comment(0)
C
2

I think you can simply override your project.lock.json with the origin one and commit.

Candlenut answered 24/4, 2016 at 12:26 Comment(0)
D
0

If they are already committed, there is no easy way that I can think of. Probably the easiest way, and a kind of work around, is to move them out the project folder, remove them from your git working copy, recommit so your branch doesn't have the JSON files in them. Then when you merge your JSON files won't go across.

Dandle answered 21/4, 2016 at 7:59 Comment(1)
Would it not show this change as "file deleted"? If I delete this file altogether and then merge my pull request it would delete this file from the main branch as well, right?Scenography
S
0

You can checkout master and pull and then rebase your branch against master and rebase master to make sure you've removed it only from your PR but not from the repo so when you merge onto master it will not remove those files but only from your PR.

git checkout master
git pull
git checkout <your-branch>
git rebase master
git push
Sherman answered 22/9, 2017 at 18:24 Comment(0)
P
0

First, find out the specific commit that affects that file. Then the below two commands should revert the commits to that file.

git revert <commit>
git push origin <branch name>
Pless answered 25/2, 2019 at 16:14 Comment(0)
P
0
  • copy the files from origin repository. And Overwrite the files in your branch's repository.
  • Make a commit.
  • Go to pull request's "Files Changed" page, there will be an option to refresh. Or refresh the page manually.
Perlman answered 10/3, 2020 at 15:40 Comment(0)
Z
0

I know this question is old, but I had exactly the same problem (a weasley YARN config file ended up in my commit without me noticing and ended up pushed to the remote and in a Pull request before I noticed

Eek.

Luckily I found this:

Git: Remove committed file after push

What worked for me was this:

git rm --cached /path/to/file
git commit -am "Remove file"
git push

It's clear, straightforward and doesn't involve complex and worrying HEAD^ commands.

To be clear, this creates another commit with the offending file removed - which is fine, although you will want to do this before the branch is merged with another - e.g. master.

Hope this helps.

Zsazsa answered 4/2 at 23:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.