如何安全地推送到本地git存储库?

时间:2017-08-11 10:08:02

标签: git

我们有两个git存储库ab。两者都是本地存储库。我如何推送回购b以回购a并回复a以回购b

# remove and create new '/tmp/repos'
rm -rf /tmp/repos
mkdir -p /tmp/repos

# create (init) and config repo 'a'
git init /tmp/repos/a
git -C /tmp/repos/a config user.email "a@example.com"
git -C /tmp/repos/a config user.name "name_a"

# clone and config repo 'a' to repo 'b'
git clone /tmp/repos/a /tmp/repos/b
git -C /tmp/repos/b config user.email "b@example.com"
git -C /tmp/repos/b config user.name "name_b"

# create file './text.txt' in repo 'b', add it, and commit it
echo "first line" > /tmp/repos/b/test.txt
git -C /tmp/repos/b add ./test.txt
git -C /tmp/repos/b commit ./test.txt -m "add first line"

我到目前为止尝试的是将a设置为裸存储库,从b推送到a,然后将a重置为非裸存储库:

git -C /tmp/repos/a config --bool core.bare true
git -C /tmp/repos/b push origin master
git -C /tmp/repos/a config --bool core.bare false

但这不是一个解决方案,因为两个存储库都必须有一个工作目录,我们不能依赖于另一个存储库的状态。

请注意,我希望从回购b推送到回购a (或类似的东西)。我知道我可以从回购b提取回购a。但这是我需要的东西。

git -C /tmp/repos/a pull /tmp/repos/b

顺便说一句,目前我们正在使用中介本地回购c。 我们正在寻找一种避免这种第三(常见)存储库的方法。 没有中央存储库的真实分布式。 我理解使用公​​共存储库是安全,健壮和简单的。 我感兴趣的是了解它如何在没有它的情况下工作, 并且当两个具有工作树的存储库直接通信时,了解问题所在。 换句话说:如果它不安全,健壮和简单,我想真正理解为什么。

1 个答案:

答案 0 :(得分:0)

如果我理解正确的话,你在本地有2个repos(简化),如下所示:

a
|- .git
|- foo.txt

b
|- .git
|- bar.txt

现在你想从回购A推回到回购B?这是不寻常的设置,因为通常你不会推送到工作仓库,而是推送到远程仓库。所以在这种情况下你会创建第三个回购:

mkdir c.git && cd c && git init --bare

您可以通过以下方式将该回购用作“远程”:

git remote add origin /path/to/c.git

然后您可以像使用任何其他远程仓库一样使用它。

但是如果你真的想推送到工作回购,那么你需要推送到.git文件夹,所以你的命令看起来像:

git push /path/to/b/.git master

但请记住,这只会更新Git目录,并且工作树将保持不变,直到有人来git checkout master