从git中的分支间合并中排除一些文件 [英] Exclude some files from inter-branch merges in git

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

问题描述

我有几个文件(一个pom.xml和一个appengine-web.xml(GAE配置文件)),我希望在同一个项目的不同分支中保持不同状态。



之前已经提出过类似的问题,以前我见过的两个解决方案是:


  • git merge --no-commit from-branch

  • 创建文件 .gitattributes 在同一个目录下,用这一行: filename.ext merge = ours (释义)


如果合并期间发生冲突,这两种方法仅保护当前分支中的文件不合并。在FF合并的情况下,当前分支中的版本被覆盖。我希望这些文件即使在FF合并的情况下也不会被覆盖。



没有办法保护当前分支中的版本不被覆盖通过合并,除非由文件特定的命令调用?



我目前诉诸不检查每个分支文件,把它们放在藏匿处,然后恢复从那里回到分支。有没有更高效,更容易出错的方法?

解决方案

另一种方法是:


  • 不是 track pom.xml

  • track pom.xml.master pom.xml.mybranch :这样,任何合并都不会影响它们,因为它们的命名方式不同。



然后,您可以进行版本和跟踪:


  • 每个分支一个pom.xml(在所述分支后面有一个命名约定)。
  • 脚本能够根据当前分支采取正确的 pom.xml。< abranch> 文件,并生成 pom.xml 文件(它保持未跟踪,私有)

  • a .gitignore 忽略生成的 pom.xml 文件

  • a .gitattribute 声明(见下文)



生成实际的 pom.xml

(图片从自定义Git - Git属性 a> from Pro Git book ))



一旦您在本地配置中声明了内容文件管理器驱动程序,它会自动在 git checkout 上生成 pom.xml 文件。

查看最佳实践 - Git + Build自动化 - 保持配置分离


$ b appengine-web.xml 文件。


I have a couple of files (a pom.xml and an appengine-web.xml (GAE config file)) that I want to keep in different states in different branches of the same project.

Similar questions have been asked before and two of the solutions I've seen suggested before are:

  • git merge --no-commit from-branch, and,
  • Create file .gitattributes in the same dir, with this line: filename.ext merge=ours (paraphrasing)

Both these only protect the file in the current branch from merges if there is a conflict during merge. In case of an FF merge, the version in the current branch gets overwritten. I want these files to not be overwritten even in the case of an FF merge.

Isn't there a way to protect the version in the current branch from ever being overwritten by a merge, unless invoked by a file-specific command?

I am currently resorting to not checking in the per-branch files, putting them on the stash and then restoring from there when I come back to the branch. Is there a more efficient and less fault-prone way?

解决方案

An alternative approach is to:

  • not track pom.xml
  • track pom.xml.master and pom.xml.mybranch: that way, any merge won't affect them, since they are named differently.

Then, you can version and track:

  • one pom.xml per branch (with a naming convention following said branch).
  • a script able to take the right pom.xml.<abranch> file depending on the current branch, and generate the pom.xml file (which remains untracked, private)
  • a .gitignore which ignores the resulting generated pom.xml file
  • a .gitattribute declaring a smudge content filter (see below)

The generation of the actual pom.xml is automated through a content filter driver, using a .gitattributes declaration.


(image from "Customizing Git - Git Attributes" from "Pro Git book"))

Once you declare that content filer driver in your local config, it will automatically, on git checkout, generate your pom.xml file for you.
See a complete example in "Best practice - Git + Build automation - Keeping configs separate".

Repeat the same idea for the appengine-web.xml file.

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

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