Git:合并公共和私有分支,同时在两个分支中保持某些文件完好无损 [英] Git: merging public and private branches while while keeping certain files intact in both branches

查看:21
本文介绍了Git:合并公共和私有分支,同时在两个分支中保持某些文件完好无损的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了一些 git 问题,但找不到这个问题的答案:

I've read a few git questions here, but could not find an answer to this one:

我有一个公共分支和一个私有分支,我想在其中允许某些文件分流.

I have a public and a private branches where I want to allow certain files to diverge.

那些是带有密码和我的本地自定义的配置文件.

Those are configuration files with passwords and my local customizations.

我确实希望能够以两种方式合并分支:从私有到公共再返回,但我不想让这些特定文件自动合并.

I do want to be able to merge the branches both ways: from private to public and back, but I do not want to ever have those specific files merged automatically.

有没有办法以这种方式设置 git?我很想找到一个自动化的解决方案 :) - 这样合并就可以像往常一样完成.

Is there a way to set up git this way? I'd love to find an automated solution :) - so that merging could be done as usual.

这是对我有用的解决方案(感谢 VonC 对 gitattribute 的建议)

here's the solution that worked for me (Thanks to VonC for the advice on gitattribute)

对我来说唯一出乎意料的是,合并保护"仅在文件在两个分支中发生分歧后才开始工作,而不是在应用以下配置后立即生效

the only unexpected thing for me was that "merge protection" starts working only after files have diverged in the two branches, not immediately after the following configuration was applied

.gitattributes(如果你想分享这个,用 git 跟踪)或 .git/info/attributes:

.gitattributes (track with git if you want to share this) or .git/info/attributes:

file1      merge=keepmine
path/file2     merge=keepmine

keepmine 是命名的自定义合并管理器,它只是一个调用的无操作命令,而不是所选文件的内部合并驱动程序,其设置如下

keepmine is the named custom merge manager that is just a do-nothing command called instead of the internal merge driver on selected files, which is set up below

当从私有分支合并到公共分支时,我通常会执行 git merge --squash private.这样私人编辑就不会进入公共分支上的 git 历史记录.

When merging from private to public branch I usually do git merge --squash private. That way private edits won't get into the git history on the public branch.

.git/config:

.git/config:

#public repository
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = <public repo git url> 

#private repository
#has to set up with git init and populated with the initial commit to branch mybranch
[remote "private"]
    push = +:
    url = /path/to/local/private/repo 
[merge "keepmine"]
    name = dont_merge_selected_files
    driver = echo %O %A %B 
[branch "master"]
    remote = origin
    merge = refs/heads/master 

#private branch settings
[branch "mybranch"]
    remote = private
    merge = refs/heads/mybranch

如果有改进的方法请评论

if there's a way to improve this please comment

推荐答案

为了安全起见,您可以添加 git 属性(见 此处举个例子) 用于那些私人文件.

To be on the safe side, you can add a git attribute (see here for an example) for those private files.

这样,您可以定义一个脚本(合并管理器"),如果在公共分支上合并,它将确保包含私人信息的文件保持为空(或具有公共内容),同时如果合并则保留其本地内容到私人分支机构.
这意味着您可以在不考虑该文件的情况下合并/rebase.

That way, you can define a script (a "merge manager") which will ensure the file including private informations will remain empty (or with a public content) if merged on the public branch, while keeping its local content if merged to the private branch.
It means you can merge/rebase without thinking about that file.

这篇关于Git:合并公共和私有分支,同时在两个分支中保持某些文件完好无损的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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