如何让第三方针对我们的git树的一部分进行开发?

问题描述

| 我们在项目中使用git。我们有一个承包商将处理一些代码,但我们希望阻止它们访问敏感的源文件和目录的某些特定列表。我对实现此目标的方式的感觉是在新服务器上创建主仓库的分支,并仅允许承包商访问此第二仓库。不知何故,我们将从第二个存储库中排除敏感文件/目录-但我需要知道如何。 (我们的一个想法是在原始存储库上创建一个分支,从该存储库中手动删除这些文件,然后仅从该分支中​​进行派生?)我们一个值得信赖的开发人员会定期将所有更改从原始存储库提取到辅助副本中,而且还会定期将我们承包商的更改推送到原始存储库中。 有人可以帮助我确定确切的方法吗?我想确保我们的安全目标达到100%。 我的研究促使我尝试以下方法。到目前为止,这是我的设置步骤,我希望这些步骤正确无误:
# We host our repo on our own server,which we all share access to via SSH.
# On this shared server we keep our git repo under:
#   /git/main-repo.git
cd /git

# make a brand-new repo which will eventually be copied to a new server
# our contractor will be given access to this repo only
git init --bare --shared restricted-repo.git

# Now push the old repo (master branch only - he won\'t need our other branches) into the new empty repo
cd /git/main-repo.git
git push /git/restricted-repo.git \'master:master\'

cd  /git/restricted-repo.git
# remove anything to do with our sensitive files.  Assume they start with \"sensitive\" in their filename
git filter-branch -f --index-filter \'git rm --cached --ignore-unmatch src/sensitive*\' HEAD

# from my research these cmds seem necessary.  I wonder if it\'s valid to run the from the bare repo,but 
# git allows this with no errors so I hope it worked.  The commands return almost instantly.
git for-each-ref --format=\"%(refname)\" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=Now --all

# this clearly does work,and takes a while
git gc --aggressive --prune=Now
现在,我有两个存储库,可以从每个存储库进行克隆和工作,但是我不知道如何在主存储库和受限存储库之间同步更改。我尝试添加一个作为另一个的远程对象并从上游拉入,但这会引入主服务器的所有分支,并且当我在两个地方都进行编辑时(即使编辑位于不同的文件中)也会造成合并冲突。     

解决方法

        如果您对存储库的一部分进行不同的管理,则它应该是不同的存储库。 您不能同时删除对这些文件的访问和痕迹,并且不能在单个存储库中维护共同的历史记录。     ,        克隆您的仓库。不要裸露,因为它会限制您可以使用滤镜分支。
git clone /c/your/path/to/repo
进入回购
cd !$:t 
失去与您从中克隆的仓库的连接
git remote rm origin
创建一个名为for-external的分支。
git checkout -b for-external
现在运行git filter-branch只保留您要共享的目录。
git filter-branch --subdirectory-filter path/to/your/project/to/share
清理
git branch | grep -v \'external\' | xargs git branch -D
git gc --prune=now
将此对象克隆到一个不受干扰的位置,以使他可以使用它。现在,您可以通过添加此新取消混入作为您的个人存储库的远程设备,来获取他所做的更改。
git remote add unfuddle <url to your unfuddle repo>
git push -u unfuddle for-external:integration
现在,您可以提取他的更改,并将其重新设置在您使用的分支之上-也许是一个名为“ from-external \”的新分支。然后,您可以将他的更改合并到常规分支中。这可以通过常规存储库完成,而不是最初克隆的存储库。
git remote add unfuddle <url to your unfuddle repo>
git fetch unfuddle
git branch -t from-consultant unfuddle/integration
请勿强行从这里解开,否则您可能会无意中推入其中包含其他信息的分支。您可以创建另一个用户,然后使用不同的ssh密钥来确保对该存储库中未融合存储库的只读访问。 您可以在上一个远程命令中使用一个替代URL,该替代URL对应于〜/ .ssh / config条目,该条目会将其映射回未混淆的URL,但提供另一个具有只读限制的键。 现在,当您在原始存储库中“ 9”时,您可以获得他的最新提交。如果您想为他贡献工作,则需要从另一个存储库中推送,这将需要您再次添加遥控器-但要小心。 过去这对我非常有用。