tavier
tavier

Reputation: 1794

How to remove a file from Git Pull Request

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?

Upvotes: 18

Views: 57867

Answers (8)

doctordjbrown
doctordjbrown

Reputation: 41

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.

Upvotes: 0

NullByte08
NullByte08

Reputation: 1042

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

Upvotes: 0

Chaitanya Vardhan
Chaitanya Vardhan

Reputation: 101

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>

Upvotes: 0

icoca
icoca

Reputation: 1

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

Upvotes: 0

Shiyason
Shiyason

Reputation: 781

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

Upvotes: 17

kofifus
kofifus

Reputation: 19315

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

Upvotes: 2

Factory Girl
Factory Girl

Reputation: 907

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.

Upvotes: 2

mrains2k
mrains2k

Reputation: 11

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.

Upvotes: 0

Related Questions