git 如何通过vscode推送多个分支到多个仓库

ryevplcw  于 2023-04-28  发布在  Git
关注(0)|答案(2)|浏览(437)

我有一个包含3个分支的仓库,名为1,2,3,以及一个公共GitHub仓库名为A,一个私有GitHub仓库名为B,一个GitLab仓库名为C。
分支3是私有的。
所以我希望VSCode在我点击按钮时为我做这件事。

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

GitHub marketplace提供了很多同步操作,但是.workflow文件夹也会同步到公共存储库,或者如果我排除了正在执行的文件夹,来自不同存储库的提交SHA将不匹配。
它通过git remote set-url --add --push工作得很好,除了分支3也被推。
我想git pre-push钩子可能会起作用,但是如何区分哪个分支是当前分支,如果我使用git push --all会发生什么?

voj3qocg

voj3qocg1#

它通过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 remote A
git remote set-url --add --push A https://github.com/username/repoA.git
git remote set-url --add --push A https://github.com/username/repoB.git
git remote set-url --add --push A https://gitlab.com/username/repoC.git

# Configure remote B
git remote set-url --add --push B https://github.com/username/repoA.git
git remote set-url --add --push B https://github.com/username/repoB.git

# Configure branch 1
git config --local "branch.branch1.remote" A
git config --local "branch.branch1.merge" refs/heads/branch1
git config --local "branch.branch1.push" refs/heads/branch1

# Configure branch 2
git config --local "branch.branch2.remote" A
git config --local "branch.branch2.merge" refs/heads/branch2
git config --local "branch.branch2.push" refs/heads/branch2

# Configure branch 3
git config --local "branch.branch3.remote" B
git config --local "branch.branch3.merge" refs/heads/branch3
git config --local "branch.branch3.push" refs/heads/branch3

当一个分支被 checkout 时的推送应该将该分支推送到正确的存储库。
但是,VSCode中的“Synchronize Changes”按钮和“Git: Push”命令仅推送 current 分支。
您可能需要创建一个自定义任务来实现您想要的(一次推送所有三个分支)

  • 使用Ctrl+Shift+P(或macOS上的Cmd+Shift+P)打开命令面板,键入“Tasks: Open User Tasks”,然后按Enter键
  • 选择“其他”
  • 替换任务的内容。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 分支获取和推送更改。
不过,您可以创建一个自定义键盘快捷键来快速运行“push-all-branches”任务。

q43xntqr

q43xntqr2#

我的.git/config在执行VonC提供的命令后的内容。

[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

当我 checkout 一个分支并运行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的P.S.命令结果

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

相关问题