使 Git 在分支之间保留不同的部分内容 [英] Making Git retain different section content between branches

查看:24
本文介绍了使 Git 在分支之间保留不同的部分内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的雇主要求我开始通过 Git 管理的用户脚本.

I'm developing a Userscript that my employers have asked me to begin to manage via Git.

现在,我有一个稳定的文件和一个 beta 文件,这样组织中的每个人都可以安装稳定的代码,但如果他们愿意,可以选择帮助测试 beta 添加.该文件的某些部分应该保持不同,内容和更改不应在分支之间合并.

Right now, I have a stable file and a beta file, so that everyone in the organization can install the stable code but can choose to help test the beta additions instead, if they want. Some portions of that file should remain different, the content and changes should not be merged between branches.

例如,如果我将 Beta 文件转换为 Git 分支,然后确定 Beta 更改是稳定的并将 Beta 合并回稳定代码(不会更改),那么我理解的 Git Merge 过程将根据 Beta 分支中这些行上的任何值帮助"更新 Stable Greasemonkey 定义标头.这是完全不可取的,因为这些标头包含一个自动更新 URL,Greasemonkey 将检查更新.

For example, if I convert the Beta file to a Git Branch, and later decide that the Beta changes are stable and merge the Beta back into the Stable code (which will not have changed) the Git Merge process as I understand it will "helpfully" update the Stable Greasemonkey definition headers based on whatever values are on those lines in the Beta branch. This is thoroughly undesirable, as these headers contain an auto-update URL that Greasemonkey will check for updates.

// ==UserScript== (stable)
// @downloadURL  --  StableURL File Location
// ==/UserScript==

// ==UserScript== (beta)
// @downloadURL  --  BetaURL File Location
// ==/UserScript==

>Git Merge<

// ==UserScript== (stable)
// @downloadURL  --  BetaURL File Location
// ==/UserScript==

我想保留在 Beta 代码和稳定代码之间具有不同 URL 的能力,但无法确定一种方法来使 Git 的合并过程忽略 Greasemonkey 正确执行其操作所需的行,但是如果我没有将 Beta 作为单独的分支,我不确定如何使用 Git 轻松地将更改后的代码从 Beta 迁移到稳定版,这就是要求我采用 Git 功能的明确原因.(嗯,另一个原因是让其他人更容易为项目做出贡献并确定项目的历史......)

I want to retain the ability to have distinct URLs between the Beta code and the Stable code, but have not been able to identify a method to make Git's merge process ignore the lines that Greasemonkey needs to do its thing properly, but if I don't have the Beta as a separate Branch, I'm not sure how to use Git to easily migrate changed code from Beta to Stable, which is the stated reason for asking me to adopt Git functionality. (Well, the other reason is to make it easier for others to contribute to and identify the history of the project...)

非常感谢任何帮助.

推荐答案

除了对那些值的更改之外,您的所有更改都应该合并,这会使它们与其他值不同,不是对内在内容的更改,而是针对特定部署的更改.这些可能最好应用于结帐后挂钩.这是一个示例,每个分支包含处理器

All your changes should be merged except changes to those values, which makes them not like the others, not changes to intrinsic content but deployment-specific changes. Those might be best applied in the post-checkout hook. Here's a sample, a per-branch include processor

cat <<EOF >.git/hooks/post-checkout
#!/bin/sh
if branch=`git symbolic-ref HEAD --short -q`; then
    for file in `git ls-files -cix*.@branch`; do
        echo "* making ${file%.@branch} from $file with branch-specific includes"
        echo '/^@include-branch-specific ([a-z/]*)$/ { s//cat 1.'$branch'/e }' 
        | sed -rf- $file >${file%.@branch}
    done
fi
EOF
chmod +x .git/hooks/post-checkout

# testing
git checkout beta

cat  <<EOF >config.@branch
// ==UserScript==
@include-branch-specific config
// ==/UserScript==
EOF

echo >config.stable '// @downloadURL  --  StableURL File Location'
echo >config.beta   '// @downloadURL  --  BetaURL File Location'

git add config.*
# git rm --cached config
git commit -m'setting up per-branch configs'
git checkout

git checkout stable
git cherry-pick beta
git checkout

这篇关于使 Git 在分支之间保留不同的部分内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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