git 拉取请求,忽略一些文件更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28703140/
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 02:57:20  来源:igfitidea点击:

Pull Request, ignore some file changes

gitgithubpull-request

提问by LongYang0806

When I do a Pull Request on GitHub (against master branch), can we ignore some file changes, like

当我在 GitHub 上(针对 master 分支)执行 Pull Request 时,我们是否可以忽略一些文件更改,例如

  • we have one file named 'fileA' in branch 'release', and we have the same file in 'master', but we do some changes in 'fileA' in branch 'release'
  • when we do a Pull Request, is there any way we can ignore the changes in 'fileA', do not let that merge into 'master'.
  • 我们在分支“release”中有一个名为“fileA”的文件,在“master”中有相同的文件,但我们在分支“release”中的“fileA”中做了一些更改
  • 当我们执行 Pull Request 时,有什么办法可以忽略“fileA”中的更改,不要让它合并到“master”中。

采纳答案by Raj Srivastava

You can't ignore some files from a pull request selectively. Two workarounds for this can be -

您不能有选择地忽略拉取请求中的某些文件。对此的两种解决方法可以是 -

First -

第一的 -

  • Create a new branch from 'release'
  • Replace the non-required files from 'master'
  • Create pull request from this new branch
  • 从“发布”创建一个新分支
  • 从“master”替换不需要的文件
  • 从这个新分支创建拉取请求

Second -

第二 -

  • Create a new branch from 'master'
  • Put changes of required files from 'release'
  • Create pull request from this new branch
  • 从'master'创建一个新分支
  • 从“发布”中放置所需文件的更改
  • 从这个新分支创建拉取请求

Any of this method will work. Which will be easier depends upon how many files are to be included / excluded.

这种方法中的任何一种都会起作用。哪个更容易取决于要包含/排除的文件数量。

回答by hlcs

Create branch with last commit you agree with:

使用您同意的最后一次提交创建分支:

git branch my-branch <sha>
git checkout my-branch

Select commits you want to pull request as patches:

选择您想要作为补丁拉取请求的提交:

git format-patch -10 <sha> --stdout > 0001-last-10-commits.patch

Apply patches:

应用补丁:

git am < 0001-last-10-commits.patch

Your commits will be as they was. You can git push -u origin my-branchimmediately.

您的提交将保持原样。git push -u origin my-branch马上就可以了。

回答by Pishu96

As mentioned in https://stackoverflow.com/a/28703636/12138397, it is not possible to exclude files in a Pull-Request, and the alternatives which have been proposed also work.

正如在https://stackoverflow.com/a/28703636/12138397 中提到的,不能在 Pull-Request 中排除文件,并且已经提出的替代方案也有效。

But there is a simpler solution to it. Here I am considering stagingas my target and devas my source

但是有一个更简单的解决方案。在这里,我考虑将登台作为我的目标,将开发作为我的源

root
|-- src
|   -- app.py
|-- .gitignore
|-- settings.py
|-- requirements.txt

Let's say, I would want to ignore the settings.pyfile from being merged

比方说,我想忽略settings.py文件被合并

  • First move to the target branch(the branch to which you want to merge the changes)
    git checkout staging
    
  • Then you can use the git checkoutcommand to selective pick the files you want to merge
    git checkout dev src/
    
  • This will only merge the files changed inside src/folder

    NOTE: You can also do it selectively for each file.

  • Then push to remote repository

    git push origin staging
    
  • 首先移动到目标分支(要将更改合并到的分支)
    git checkout staging
    
  • 然后你可以使用git checkout命令来选择性地选择你想要合并的文件
    git checkout dev src/
    
  • 这只会合并src/文件夹中更改的文件

    注意:您也可以有选择地为每个文件执行此操作。

  • 然后推送到远程仓库

    git push origin staging
    

But this solution is useful only if the files to be excluded is small.

但此解决方案仅在要排除的文件较小时才有用。