Git:在合并期间忽略一些文件(将一些文件限制在一个分支) [英] Git: ignore some files during a merge (keep some files restricted to one branch)

查看:60
本文介绍了Git:在合并期间忽略一些文件(将一些文件限制在一个分支)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个分支,AB.分支A 有一个目录examples 包含一些git 跟踪的文件,这些文件应该出现在分支B.在我的工作流程中,我经常将 A 中所做的更改合并到 B 中,这在每次 examples 上有一些更改时都会出现问题.目前我正在手动执行此操作:在合并后删除文件或在我已经删除的文件发生更改时解决冲突.

是否可以在合并期间忽略这些文件?(或者是否可以将某些文件限制在一个分支(A)或远离一个分支(B)?)

<小时>

让我试着解释一下我这样做的原因:A 是博客的骨架(模板、脚本等),B 是我的博客(A 充满了我自己的帖子、图片、草稿等).A 是公开的,我试图让它对其他人看起来和使用它是通用的,但因此我需要一些帖子作为展示/测试(examples 目录).A 中的每个更改,后来都合并到 B 中,以便在我的博客实例上进行这些更改——这样所有新示例都出现在 BB 中所有已删除的示例自上次合并后在 A 中发生更改都会导致冲突.

解决方案

我在这里找到了一个很好的答案:stackoverflow Q332528

它使用了从这里获取的想法:Pro-Git 合并策略

这是它的副本:

<块引用>

假设您要排除文件 config.php

在分支 A:

  1. 在同一个目录中创建一个名为.gitattributes"的文件,用这个行:config.php merge=ours.这告诉 git 使用什么策略合并文件时.在这种情况下,它始终保留您的版本,即.您要合并到的分支上的版本.

  2. 添加 .gitattributes 文件并提交

在分支 B:重复步骤 1-2

现在尝试合并.您的文件应该保持不变.



来自 git book 关于 merge=ours,一个非常有用的选项是告诉 Git 在发生冲突时不要尝试合并特定文件,而是使用你的合并而不是其他人的合并.">

因此,此答案并不适用于该问题.pjmorse 的回答关于使用子模块很好.

另一种选择是使用子树合并,这可能会带来额外的好处.

I have two branches, A and B. Branch A have a directory examples with some files that are tracked by git, and these files should not appear on branch B. In my workflow, I do merge changes made in A into B often, which is a problem every time that there is some changes on examples. For the moment I am doing this manually: erasing the files after the merge or solving conflicts when there was a change to a file that I had already erased.

Is it possible to ignore these files during a merge? (Or is it possible to keep some files restricted to one branch (A) or away from one branch (B)?)


Let me try to explain why I am doing this: A is a skeleton of a blog (template, scripts, etc), B is my blog (A filled with my own posts, images, drafts, etc). A is public and I am trying to make it generic to others look and use it, but because of this I need some posts there as a showcase/tests (the examples directory). Every change in A and is later merged into B to have this changes on my blog instance -- this way all new examples appear in B and all deleted examples in B that have been changed in A since last merge results in a conflict.

解决方案

I found a good answer here: stackoverflow Q332528

It uses ideas taken from here: Pro-Git merge strategies

Here is a copy of it:

Let's say you want to exclude the file config.php

On branch A:

  1. Create a file named '.gitattributes' in the same dir, with this line: config.php merge=ours. This tells git what strategy to use when mergin the file. In this case it always keep your version, ie. the version on the branch you are merging into.

  2. Add the .gitattributes file and commit

On branch B: repeat steps 1-2

Try merging now. Your file should be left untouched.


Edit:
From the git book regarding merge=ours, "One very useful option is to tell Git to not try to merge specific files when they have conflicts, but rather to use your side of the merge over someone else’s."

So, this answer doesn't apply as well as it might to the question. pjmorse's answer regarding using submodules is good.

Another option would be to use a sub-tree merge, which may have added benefits.

这篇关于Git:在合并期间忽略一些文件(将一些文件限制在一个分支)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆