如何通过 vscode 将多个分支推送到多个存储库

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

我有一个包含 3 个分支的 repo,名为 1、2、3,还有一个名为 A 的公共 GitHub 存储库,一个名为 B 的私有 GitHub 存储库,一个名为 C 的 GitLab 存储库。
分支 3 是私有的。

所以我希望 VSCode 在我单击按钮时为我执行此操作。

  • 将分支 1,2 推送到 A,B,C
  • 将分支 3 推送到 B,C

GitHub marketplace 提供了很多同步操作,但是

.workflow
文件夹也会同步到公共存储库,或者如果我在操作中排除文件夹,来自不同存储库的提交 SHA 将不匹配。

它通过

git remote set-url --add --push
工作得很好,除了分支3也被推了。

我认为 git pre-push hook 可能会起作用,但是如何区分哪个分支是当前分支以及如果我使用它会发生什么

git push --all

git github-actions githooks
2个回答
0
投票

通过

git remote set-url --add --push
工作正常,除了分支 3 也被推入

不应该,因为您可以列出要为每个分支推送的存储库。

例如假设有3个仓库:

git remote add A https://github.com/username/repoA.git
git remote add B https://github.com/username/repoB.git
git remote add C https://gitlab.com/username/repoC.git

您可以配置每个分支:

# Configure branch 1
git config --local "branch.branch1.remote" A
git config --local "branch.branch1.merge" refs/heads/branch1
git remote set-url --add --push A refs/heads/branch1:refs/heads/branch1
git remote set-url --add --push B refs/heads/branch1:refs/heads/branch1
git remote set-url --add --push C refs/heads/branch1:refs/heads/branch1

# Configure branch 2
git config --local "branch.branch2.remote" A
git config --local "branch.branch2.merge" refs/heads/branch2
git remote set-url --add --push A refs/heads/branch2:refs/heads/branch2
git remote set-url --add --push B refs/heads/branch2:refs/heads/branch2
git remote set-url --add --push C refs/heads/branch2:refs/heads/branch2

# Configure branch 3
git config --local "branch.branch3.remote" B
git config --local "branch.branch3.merge" refs/heads/branch3
git remote set-url --add --push B refs/heads/branch3:refs/heads/branch3
git remote set-url --add --push C refs/heads/branch3:refs/heads/branch3

签出分支时的推送应该将该分支推送到正确的存储库。

然而,VSCode中的“

Synchronize Changes
”按钮和“
Git: Push
”命令仅推送current分支。

您可能需要创建一个自定义任务来实现您想要的(一次推动所有三个分支)

  • 使用 Ctrl+Shift+P(或 macOS 上的 Cmd+Shift+P)打开命令面板,输入“
    Tasks: Open User Tasks
    ”,然后按 Enter
  • 选择“其他”
  • 将tasks.json的内容替换为:
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "push-all-branches",
      "type": "shell",
      "command": "git checkout branch1 && git push && git checkout branch2 && git push && git checkout branch3 && git push",
      "group": "none",
      "presentation": {
        "reveal": "always",
        "panel": "shared"
      }
    }
  ]
}

这样,你就有了一个“

push-all-branches
”的新命令。

请注意,您不能将自定义任务直接关联到 Visual Studio Code 中的“

Synchronize Changes
”按钮,因为它被硬编码为执行“
Git: Sync
”命令,该命令仅针对current获取和推送更改分行

不过,您可以创建自定义键盘快捷键来快速运行“推送所有分支”任务。


0
投票

执行VonC提供的命令后我

.git/config
的内容

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    ignorecase = true
[remote "A"]
    url = https://github.com/username/repoA.git
    fetch = +refs/heads/*:refs/remotes/A/*
    pushurl = refs/heads/branch1:refs/heads/branch1
    pushurl = refs/heads/branch2:refs/heads/branch2
[remote "B"]
    url = https://github.com/username/repoB.git
    fetch = +refs/heads/*:refs/remotes/B/*
    pushurl = refs/heads/branch1:refs/heads/branch1
    pushurl = refs/heads/branch2:refs/heads/branch2
    pushurl = refs/heads/branch3:refs/heads/branch3
[remote "C"]
    url = https://gitlab.com/username/repoC.git
    fetch = +refs/heads/*:refs/remotes/C/*
    pushurl = refs/heads/branch1:refs/heads/branch1
    pushurl = refs/heads/branch2:refs/heads/branch2
    pushurl = refs/heads/branch3:refs/heads/branch3
[branch "branch1"]
    remote = A
    merge = refs/heads/branch1
[branch "branch2"]
    remote = A
    merge = refs/heads/branch2
[branch "branch3"]
    remote = B
    merge = refs/heads/branch3

当我检查一个分支并运行

git push
时,我得到了这样的错误。

fatal: 'refs/heads/branch1:refs/heads/branch1' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我不知道为什么会这样。但这导致我提交

.git/config
。后 深入研究它,我将我的
.git/config
修改为:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "private"]
    url = https://github.com/username/repoA.git
    fetchurl = https://github.com/username/repoA.git
    fetch = +refs/heads/*:refs/remotes/private/*
    pushurl = https://github.com/username/repoA.git
    pushurl = https://github.com/username/repoB.git
[remote "public"]
    url = https://github.com/username/repoA.git
    fetchurl = https://github.com/username/repoA.git
    fetch = +refs/heads/*:refs/remotes/public/*
    pushurl = https://github.com/username/repoA.git
    pushurl = https://github.com/username/repoB.git
    pushurl = https://gitlab.com/username/repoC.git
[branch "branch1"]
    remote = public
    merge = refs/heads/branch1
    push = refs/heads/branch1
[branch "branch2"]
    remote = public
    merge = refs/heads/branch2
    push = refs/heads/branch2
[branch "branch3"]
    remote = private
    merge = refs/heads/branch3
    push = refs/heads/branch3

这对我有用。

附言

git branch -avv

的命令结果
PS C:\Repo> git branch -avv
* branch2                       e447c9a [public/branch2] fix: msg1
  branch3                       1a2dd91 [private/branch3] fix: msg2
  branch1                       167db89 [public/branch1] msg4
  remotes/repoB/branch2         df83934 feat: msg3
  remotes/repoB/branch3         1a2dd91 fix: msg2
  remotes/repoB/branch1         167db89 msg4
  remotes/repoC/branch2         e447c9a fix: msg1
  remotes/repoC/branch3         1a2dd91 fix: msg2
  remotes/repoC/branch1         167db89 msg4
  remotes/repoA/branch2         df83934 feat: msg3
  remotes/repoA/branch1         167db89 msg4
  remotes/private/branch2       e447c9a fix: msg1
  remotes/private/branch3       1a2dd91 fix: msg2
  remotes/private/branch1       167db89 msg4
  remotes/public/branch2        e447c9a fix: msg1
  remotes/public/branch3        1a2dd91 fix: msg2
  remotes/public/branch1        167db89 msg4
© www.soinside.com 2019 - 2024. All rights reserved.