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

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

问题描述

我有两个分支, A B 。分支 A 有一个目录 examples ,其中包含一些由git跟踪的文件,这些文件应该 not 出现在分支 B 上。在我的工作流程中,我常常将 A 中所做的更改合并到 B 中,每当存在例子的一些变化。目前,我正在手动执行此操作:在合并或解决冲突后清除文件,当我已擦除的文件发生更改时。



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






让我试着解释为什么我这样做:
A 是博客(模板,脚本等)的骨架, B 是我的博客( A 填写我自己的帖子,图片,草稿等)。 A 是公开的,我试图让其他人看起来并且使用它,但正因为如此,我需要一些职位作为showcase / tests(例子目录)。在 A 中进行每一项更改,稍后将其合并到 B 中,以便在我的博客实例上进行更改 - 这样,所有新示例出现在 B 中,并且 B 中已在 A 自上次合并导致冲突。

解决方案

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



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

下面是它的一个副本:


假设您要排除文件 config.php



在分支A上:


  1. 在同一个di上创建一个名为'.gitattributes'的文件r,用这个
    行:config.php merge = ours。这告诉git在合并文件时使用
    的策略。在这种情况下,它始终保持您的版本,即。
    您正在合并的分支上的版本。


  2. 添加.gitattributes文件并提交

  3. b

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



立即尝试合并。






编辑:

git book 关于 merge =我们的,一个非常有用的选择是告诉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天全站免登陆