有没有办法让两个分支不合并在git中 [英] Is there a way to keep two branches from merging in git

查看:192
本文介绍了有没有办法让两个分支不合并在git中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在git仓库中保留两个独立的分支,即使分支可能包含类似的内容也不能意外合并。

I would like to be able to keep two separate branches in a git repo that cannot accidentally be merged even though the branches may contain similar content.

有没有诡计迫使分支在git中保持独立?假设我有分支 A 和分支 B ,类似于 git merge B //假设A被签出将会失败。

Is there a trick for forcing a branch to remain separate in git? That is assuming I have branch A and branch B, something like git merge B //assuming A is checked out would fail.

这里引起的关注是因为在创建网站的存储库时,我需要 HEAD 主分支永远是稳定网站的当前状态。

The concern here arose because in creating a repository of a website I'm developing I need the HEAD of the master branch to always be the current state of the stable website.

任何开发提交都需要在开发分支上进行,主题分支关闭开发

Any development commits I need to occur on the development branch with topic branches off of the development branch if necessary.

 C1-C2                        master
     \
     C3-C4-C5-C6-C9-C10-C11   development
               \
                C7-C8-C12     topic/HEAD

我需要确保主分行与其他分行之间意外合并的机会很小。

I need to make sure that there's little chance of an accidental merge between the master branch and other branches.

所以我正在寻找在与主分支合并之间进行合并将在网站上进行,您确定要这样做吗?并且只有在我确认合并后,它才会通过。

So I'm looking for a way to step 'in between' a merge with the master branch to ask, "This merge will go live on the site, are you sure you want to do it?" And only after I confirm the merge will it go through.

我想这个场景仅适用于那些不需要编译以便运行的代码的web开发人员,并且可能会在实时安装中提取稳定网站的副本。

I guess this scenario is relevant only to web developers with code that does not need to be compiled in order to run, and may pull copies of the stable website on a live installation.

推荐答案

我相信唯一可以实现这一目标的方法是预先提交钩子。从手册页:

I believe the only way you could accomplish this is with a pre-commit hook. From the manpage:


该钩子由git commit调用,可以用--no-verify选项绕过。它不接受任何参数,并在获取建议的提交日志消息并进行提交之前被调用。

This hook is invoked by git commit, and can be bypassed with --no-verify option. It takes no parameter, and is invoked before obtaining the proposed commit log message and making a commit. Exiting with non-zero status from this script causes the git commit to abort.

所以,如果你真的想要,你可以编写一个预先提交的钩子,用于检查当前的分支和你正试图合并的分支,如果它不是你想要的那一对,则以非零状态退出。除了我的头顶,我可以想到检查你想要合并的分支的唯一方法是检查文件 .git / MERGE_MSG 并解析第一个线。 (另外, .git / MERGE_HEAD 会告诉你你正在合并的SHA1,但没有记录它是什么分支,所以如果两个分支在同一个地方,你运气不好。

So, if you really want to, you can write a pre-commit hook which checks the current branch and the branch you're attempting to merge, and exits with non-zero status if it's the pair you don't want. Off the top of my head, the only way I can think of to check what branch you're attempting to merge is to examine the file .git/MERGE_MSG and parse the first line. (The alternative, .git/MERGE_HEAD will tell you the SHA1 you're merging, but with no record of what branch it was, so if two branches are in the same place, you're out of luck.

预提交钩显然不适用于快进合并;我怀疑是否有任何方法可以防止这种情况发生。大概如果你让这些分支不合并,就不会有快速尝试。

The pre-commit hook is obviously not run for fast-forward merges; I doubt there's really any way to protect against that. But presumably if you keep the branches from being merged, there won't ever be a fast-forward attempt.

这篇关于有没有办法让两个分支不合并在git中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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