如何从 Git 拉取请求中删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36763274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:05:58  来源:igfitidea点击:

How to remove a file from Git Pull Request

gitpull-requestsourcetree

提问by tavier

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

我打开了一个拉取请求,其中有一些project.lock.json我不想在将我的分支合并到主分支时合并的文件。有没有办法project.lock.json从我的拉取请求中删除这些文件?

采纳答案by Factory Girl

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.

如果您想将文件留在分支中,但不想将其合并到主分支,则可以在一次提交中将其删除,然后在另一次提交中再次添加。Git 允许您使用 git-cherry-pick 手动接受某些提交。除了您再次添加此文件的提交之外,您可以接受每个提交。

回答by Shiyason

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

结帐 PR 分支

git checkout origin pr_branch

overwrite pr_branch's file with other_branch's file

用 other_branch 的文件覆盖 pr_branch 的文件

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

回答by kofifus

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

我认为你可以简单地用原点覆盖你的 project.lock.json 并提交。

回答by mrains2k

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.

如果他们已经承诺,我想不出简单的方法。可能最简单的方法和一种变通方法是将它们移出项目文件夹,从您的 git 工作副本中删除它们,重新提交以便您的分支中没有 JSON 文件。然后当你合并你的 JSON 文件时就不会出现了。

回答by icoca

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.

您可以检出 master 和 pull 然后根据 master 和 rebase master 重新设置分支,以确保您仅从 PR 中删除它,而不是从 repo 中删除它,因此当您合并到 master 时,它不会删除这些文件,而只会从您的 PR 中删除.

git checkout master
git pull
git checkout <your-branch>
git rebase master
git push

回答by Chaitanya Vardhan

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>

回答by NullByte08

  • 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.
  • 从原始存储库复制文件。并覆盖分支存储库中的文件。
  • 进行提交。
  • 转到拉取请求的“文件已更改”页面,将有一个选项可以刷新。或者手动刷新页面。