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

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

问题描述

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



我有一个公共和私人分支,我想要允许某些文件分歧。



这些配置文件包含密码和我的本地自定义设置。



我希望能够以两种方式合并分支:从私人到公共和后退,但我不希望自动合并这些特定文件。 / p>

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

编辑:这是我工作的解决方案(感谢VonC对gitattribute的建议)



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

.gitattributes(使用git跟踪,如果您想分享此内容)或。 git / info / attributes:

  file1 merge = keepmine 
path / file2 merge = keepmine
code>

keepmine是命名的自定义合并管理器,它只是一个无所事事的命令,而不是所选文件的内部合并驱动器,设置在

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



.git / config:

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

#私有存储库
#必须使用git init进行设置,并使用分支mybranch
[remote'private']
push = +:
url = / path / to / local / private / repo
[mergekeepmine]
name = dont_merge_selected_files
driver = echo%O%A%B
[branchmaster]
remote = origin
merge = refs / heads / master

#private分支设置
[分支mybranch]
remote = private
merge = refs / heads / mybranch

如果有方法可以改善请评论

解决方案

为了安全起见,您可以添加一个 git attribute (请参阅这里是一个例子)对于那些私人文件。



这样,你可以定义一个脚本(一个合并管理器),它将确保包含私人信息的文件将保持为空(或与公共内容)公共分支,同时保留其本地内容,如果合并到私人分支。

这意味着你可以合并/ rebase而不考虑该文件。


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.

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.


EDIT: 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 (track with git if you want to share this) or .git/info/attributes:

file1      merge=keepmine
path/file2     merge=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

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:

#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

解决方案

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

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天全站免登陆