我应该使用Git分支来存储不相关的代码吗?

 xiaomanni521125655 发布于 2022-12-08 07:38

我对非标准git分支策略有疑问。

假设我有很多小文件,例如脚本,它们在很大程度上彼此无关。假设我将它们分成不同的概念分组,然后将每组文件依次提交到不同的空分支。因此,我将有一个空的母版,并从那里分支,例如foo文件,bar文件等,每个文件都包含完全不同的文件和文件夹结构。

其他一些要求

分支机构不必合并就可以掌握。但根据某些命名约定,它们可能彼此融合,例如:foo-files-master> foo-files-bobswork

绝对不希望使用单独的存储库,因为在某些情况下,我们只讨论概念上相关的1个或2个文件,不足以证明单独存储库的管理开销是合理的。

I am hesitant to put all the directories on the same branch, because I don't want to have to clone all the files when I am just interested in two of thousands.

Is this advisable? Will it bite me in the future?

I searched and searched for any information on this approach and didn't find much. I found 'orphan branches' which looks to be relevant. I am going test this. But ignoring that feature for a minute, is there any downside to having a bunch of branches, stemming from an empty master, each with a completely different file structure?

1 个回答
  • Like you mentioned, you can accomplish this with orphan branches.

    git checkout --orphan script-branch
    git reset --hard
    touch my-script.sh
    git add my-script.sh
    git commit -m 'Script commit'
    

    Now if you do a git log, you will only see 'Script commit' in the history.

    There is nothing inherently wrong with this approach, but there are some pros and cons:

    Pros:

    Low overhead: Your new branches are easily accessible by everyone who already has access to your repo. All you have to do is checkout the branch and you're ready to go.

    Separation: Since your scripts are in a separate branch, they can be maintained separately and have their own separate git history.

    Cons:

    Unusual organization: Typically people expect branches to be a modified version of the master branch. You will need to clarify your scheme to other developers.

    Can't access both at the same time: You will not be able to access your scripts while you have master checked out and vice-versa. You can solve this by cloning your repo twice, but that may complicate things.

    Heavy clone: You mentioned that a separate branch will save you from having to clone the whole repository, but you will do that anyway. If you run clone normally, git will clone every branch including your master branch regardless of which branch you have checked out. You can use the --single-branch flag to only clone one branch, but that would somewhat nullify the utility of having the branch in the same repo.

    Alternatives:

    Include your scripts in a directory in your master branch. Yes, this means you have to clone the entire repository just to get the scripts. But if the scripts are related to the code in your master branch, this is the simplest and most standard solution.

    Create a single new repo just for your scripts. You can then use directories to group related scripts. This saves you the overhead of having a separate repo for every set of scripts, but still allows you to clone it separately.

    2022-12-11 02:06 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有