从git中的分支间合并中排除一些文件 [英] Exclude some files from inter-branch merges in 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。< abranch>
文件,并生成 pom.xml
文件(它保持未跟踪,私有)
.gitignore
忽略生成的 pom.xml
文件
.gitattribute
声明(见下文)
生成实际的 pom.xml $ c $通过
(图片从自定义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
andpom.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 thepom.xml
file (which remains untracked, private) - a
.gitignore
which ignores the resulting generatedpom.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屋!