git - 如何从合并中排除文件 [英] git - how to exclude files from merging

查看:240
本文介绍了git - 如何从合并中排除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将2个网站项目保存在一个存储库中。这个网站除了模板(html,css)文件和几个配置文件外基本相同。
主站点(这是我所说的Supersite)在分支主站中。第二个站点在分支secondarySite中。每当我在master分支中开发一些新功能时,我想将它合并到secondarySite,但我想从合并中排除模板文件。



我在这里找到部分解决方案< a href =https://stackoverflow.com/questions/928646/how-do-i-tell-git-to-always-select-my-local-version-for-conflicted-merges-on-a-sp >如何告诉git始终选择我的本地版本来处理特定文件上的冲突合并?,但只有当我在两个分支中更改模板文件并发生冲突时,git才能正常工作。



如何让git始终保持始终保持指定的本地文件不变,即使如果没有冲突git也只是使用较新的远程文件版本。没有冲突。



或者我可能使用完全错误的方法解决问题?

预先感谢任何帮助。

解决方案

我在合并驱动程序上的答案,它们仅在发生冲突时才有用。

适用于 .gitattributes 文件中的 merge = ours :参见合并策略



一个非常有用的选择是告诉Git不要在特定文件遇到冲突时合并特定文件,而是要将合并的一方用于某种情况ne else's。


















$ b < blockquote>


有没有一种方法可以从branchA合并到branchB,并从branchB合并到
branchA,同时完全忽略对被跟踪文件
的更改,是否存在于两个分支?


没有。基本上,git中的一个提交对象由一个内容状态(即指向树对象的指针)和一个指向所有以前历史记录的指针组成(即零或多个父提交对象的指针)。
提交对象的语义可以被认为是我查看了所有父提交中的所有历史记录,并且包含在我的树指针中的状态取代它们全部。



因此,您可以将 B 合并为 A ,但保留 A 的文件副本(例如,使用我们的策略) 。但是这就是说你考虑了A和B的状态,并且认为 A 的版本取代了 B 。如果你以后想要从 A B B 的文件版本甚至不会被视为合并的结果。



有没有一种巧妙的方法可以通过不同的合并来解决这个问题战略;它是存储历史的git数据结构的一个基本方面。

如果这些模板位于子目录中,最好是隔离他们在他们自己的git仓库中,以便将该仓库作为子仓库包含。



如果没有,那么您需要在提交之前将文件错误地合并在这个帖子):

  git merge --no-commit other 
git checkout HEAD XYZ#或'git rm XYZ'if XYZ does not存在于主
git commit


I'm trying to keep 2 projects of website in one repository. This websites are mainly the same except template (html,css) files and few config files. The main site (which is my say Supersite) is in branch master. The second site is in branch secondarySite. Every time, when I develop some new feature in master branch I want to merge it to secondarySite but I want to exclude templates files from merging.

I found partial solution here How do I tell git to always select my local version for conflicted merges on a specific file? but it works only when I change template file in both branches and conflict occurs. When there is no conflict git just use newer remote version of file.

How can I tell git to always leave specified local files unchanged even if there is no conflict.

Or maybe I use totally wrong approach to the problem?

Thanks in advance for any help.

解决方案

As mention in my answer on merge drivers, they are useful in case of conflict only.
That applies to a merge=ours in a .gitattributes file: see Merge strategies.

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.

This recent thread (2012) confirms it:

Is there a way to merge from branchA to branchB and from branchB to branchA while completely ignoring changes to a file that is tracked and exists in both branches?

No. Fundamentally, a commit object in git consists of a content state (i.e., a pointer to a tree object) and a pointer to all previous history (i.e., zero or more "parent" pointers to commit objects).
The semantics of a commit object can be thought of as "I have looked at all of the history in all of the parent commits, and the state contained in my tree pointer supersedes them all".

So you could make merge B into A, but keep A's copy of the file (e.g., using the "ours" strategy). But that is saying that you considered the state of both A and B, and decided that A's version supersedes what happened in B. If you later wanted to merge from A to B, B's version of the file would not even be considered as an outcome for the merge.

There isn't really a clever way to work around this via a different merge strategy; it's a fundamental aspect of git's data structure for storing history.

If those templates are in a sub-directory, it is best to isolate them in a git repo of their own, in order to include that repo as a submodule.

If not, then you need to revert the files incorrectly merged before committing (as in this thread):

git merge --no-commit other
git checkout HEAD XYZ  # or 'git rm XYZ' if XYZ does not exist on master
git commit 

这篇关于git - 如何从合并中排除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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