git - 如何从 merge 中排除文件

标签 git merge

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

我在这里找到了部分解决方案 How do I tell git to always select my local version for conflicted merges on a specific file?但它仅在我更改两个分支中的模板文件并且发生冲突时才有效。当没有冲突时,git 只使用较新的远程文件版本。

即使没有冲突,我如何告诉 git 总是保持指定的本地文件不变。

或者也许我使用了完全错误的方法来解决这个问题?

在此先感谢您的帮助。

最佳答案

my answer on merge drivers 中所述,它们仅在发生冲突时有用。
applies to a merge=ours in a .gitattributes file :见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.

recent thread (2012)确认:

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?

没有。从根本上说,git 中的提交对象由内容状态(即指向树对象的指针)和指向所有先前历史的指针(即零个或多个“parent”指向提交对象的指针)。
提交对象的语义可以被认为是“我已经查看了所有父提交中的所有历史记录,并且我的树指针中包含的状态取代了它们”。

因此,您可以将 B merge 到 A 中,但保留 A 的文件副本(例如,使用“我们的”策略)。但这就是说您考虑了 A 和 B 的状态,并决定 A 的版本取代 B 中发生的情况。如果您以后想要从 A merge 到 BB 的文件版本甚至不会被视为 merge 的结果.

通过不同的 merge 策略并没有真正聪明的方法来解决这个问题;它是 git 存储历史数据结构的一个基本方面。

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

如果不是,那么您需要在提交之前恢复错误 merge 的文件(如 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 - 如何从 merge 中排除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6294355/

相关文章:

Git:从上一个分支再次 merge

python - 如何处理 `relation "cms_disclaimerpanel"already exists` 和 ProgrammingError : column "http_request_lang" of relation "xyz" does not exist

r - 结合get()函数实现lapply来矢量化数据表的合并?右

java - 录音时可以合并音频(音频位置 "res - raw - audio.mp3")吗?

python - Pandas:相互评估两个数据帧

git - 如何 git commit --amend 作为分支基础的提交

git - 如何停止跟踪git中目录的内容?

java - 无法将Android项目提交或推送到github,“提交”按钮被盗:(

斯卡拉 : Merge two immutable maps on key and get new immutable map with same type

Git merge 两个本地分支