首页 > 解决方案 > 发布私有 git 存储库的某些路径

问题描述

我正在与一个由约 10 名开发人员组成的团队合作,开发一个包含以下项目的私有 gitlab 存储库:

  1. 服务器
  2. 人工智能
  3. 客户
  4. 界面
  5. 杂项(协议、公关材料等)

除了Misc之外,它们中的每一个都有自己的Maven依赖项和包含在其特定文件夹中的单元测试。

我们正在使用git-flow,因此所有分支将develop在某个时候与一个分支合并。


问题:

我们目前只销售带有源代码的客户端和接口,并希望我们的客户只能访问这些(子)项目,包括他们的历史,同时能够轻松推送更新并使用 gitlab 的issues功能。

我的想法:

  1. 如果它是一个公共存储库,我会简单地使用git submodules,但这个解决方案似乎不能完美地与私有存储库一起工作。(如果有的话——已经阅读了很多关于无效路径的问题)
  2. Client如果我有and的超级干净的分支Interface,我可以添加另一个新的remote存储库并将这两个分支简单地推送到它。这个解决方案的问题是,我们有没有经验的开发人员,一次肮脏的推送或来自develop分支的一次推送基本上会使整个想法变得毫无用处。风险太高了。
  3. 我的另一个想法是将这两个子项目移出私有仓库,在私有仓库中创建子模块。这在某种程度上也让人觉得很不方便,因为我们continuous integration会在不同的 repo 中运行,而且我们自己的问题也会在那里被跟踪。

由于这是一个非常具体的设置和计划,我会对您解决此类情况的想法感兴趣。谢谢你的时间。

标签: gitgitlab

解决方案


如果找到更优雅的解决方案或遇到麻烦,我会更新这篇文章。

此解决方案还发布您项目的 git-history。不过,根据您的团队事先工作的干净程度,您可以将其过滤掉: Git Documentation on how to do this。您应该在清理分支后执行此步骤。

这个解决方案背后的想法是......

  1. 创建超级干净的分支,仅包括要部署的目录
  2. 然后将这些分支拉入一个新的存储库
  3. 控制步骤检查分支是否干净并准备好部署
  4. 部署

确保您有另一个备份并彻底测试。我们在某个时候使用了强制命令。这在我们的存储库上完美运行,但可能会导致我尚未意识到的副作用。如果您遇到它们,请告诉我,以便我可以更新此条目。

执行步骤private local repository

# Assure we are on the latest stable state within our main repo
git checkout <CURRENT_STATE>

# Create a new deployment branch from HEAD
git branch <DEPLOYMENT_BRANCH> HEAD

# Enter the new branch
git checkout <DEPLOYMENT_BRANCH>

[选项 1]:如果您打算删除目录的路径结构并将所有文件部署到新存储库的根目录中,请使用以下策略

# Filter every comment outside of this subdirectory (execute from root directory of repo)
git filter-branch -f --subdirectory-filter <DIRECTORY_PATH> -- <DEPLOYMENT_BRANCH>

[选项2]:过滤并保留目录树

##
# Force filter-branch using the tree-filter (keeps the 
# directory-structure) while deleting all files outside    
# this directory (Check if you are on <DEPLOYMENT_BRANCH>
# first). Folders are kept, but since Git does not push 
# empty folders this is no problem

git filter-branch --tree-filter -f \
    'find . -path <DIRECTORY_PATH> -prune -o -type f -print0 | xargs -0 rm -f' \
    --prune-empty HEAD

我们想添加master分支的.gitignore文件。

# Take master .gitignore and add it to the branch
git checkout master -- .gitignore

您还应该更新它以忽略所有其他目录,但是您的<DIRECTORY_PATH>

##
# In .gitignore:
# Ignore everything:
*

# Except for these directories:

!path/
!path/*/
!.gitignore

暂存并提交这些更改:

git add .gitignore
git commit -m "Add .gitignore"

通知您的开发人员在他们的 : 中跟踪新分支private local repositories

##
# If you would pull this branch, Git would attempt a merge it into your current HEAD.
# Do NOT pull! Instead start tracking the remote branch

git checkout --track origin/<DEPLOYMENT_BRANCH>

新的执行步骤destination / deployment repository

克隆或创建destination / deployment repository. 在它里面添加一个遥控器到我们的private local repository并将分支拉/合并到master中:

# Add remote to local private repo
git remote add local <PATH/TO/PRIVATE/REPO/.git>

git checkout master

[选项 1]:拉取并因此将更改直接合并到 master

# Pull changes from private repo and allow unrelated histories

git pull local <DEPLOYMENT_BRANCH> --allow-unrelated-histories

[选项 2]:在没有即时合并的情况下跟踪分支

# Track branches for more granular control
git checkout --track local/<DEPLOYMENT_BRANCH>

部署:

# Deploy option 1: (For option 2 simply push your tracked branches)
git push origin master

推荐阅读