设置gitlab存储库有超过1个远程

问题描述 投票:0回答:1

我正在使用 Gitlab 中的存储库。当我克隆存储库时,它具有通常的

remote origin
,它指向 Gitlab。但是,我还需要将提交推送到第二个存储库,因此我在克隆存储库后立即设置了第二个
remote

git remote add second <url_of_secondary_repo>

我的问题是,有没有办法将第二个遥控器存储在 Gitlab 存储库的

git config
中,这样我就不需要每次克隆存储库时添加它,而是在克隆它并查询时立即添加它
git remote -v
,我明白例如

origin  [email protected]:<user>/<repo>.git (fetch)
origin  [email protected]:<user>/<repo>.git (push)
second  <url_of_secondary_repo> (fetch)
second  <url_of_secondary_repo> (push)
git gitlab git-remote
1个回答
0
投票

我没有看到直接提交 git config 的方法,有一个使用 git hooks 的解决方法,但考虑到您的用例,我认为这不值得。虽然我还在记下来以防万一。

在 GIT 中,一旦克隆存储库,

post-checkout
钩子就会被调用。您可以将 shell 命令 (
git remote add second <url_of_secondary_repo>
) 放入此挂钩文件 (
.git/hooks/post-checkout
) 中。 Git 不允许您提交钩子,但是互联网上有很多解决方法可以帮助您在团队中共享 git 钩子。现在,一旦用户克隆此存储库,
post-checkout
钩子就会自动执行,第二个远程将被添加到他们的配置中。

请注意,每次运行 checkout 命令时都会调用 post-checkout 挂钩,因此,如果配置中不存在第二个远程,则必须检查仅运行添加远程命令。

OR

更复杂、更具表现力的做法是 -

  1. 在存储库的顶层添加一个名为
    gitconfig
    的新文件。
  2. 将下面提到的配置放入此 gitconfig 文件中。
  3. 将此命令
    git config --local include.path ../gitconfig
    粘贴到
    .git/hooks/post-checkout
    文件中。
  4. 使您的挂钩可共享。
  5. 提交gitconfig

现在,一旦有人克隆存储库,第二个远程将被自动添加,如果他们下载它而不是克隆,那么一旦他们第一次运行签出命令,第二个远程将被添加。

gitconfig 文件的内容 -

[remote "second"]
url = <url_of_secondary_repo>
fetch = +refs/heads/*:refs/remotes/second/*```
© www.soinside.com 2019 - 2024. All rights reserved.