簡體   English   中英

能夠使用一個命令推送到所有 git 遙控器嗎?

[英]Able to push to all git remotes with the one command?

而不是做:

git push origin --all && git push nodester --all && git push duostack --all

有沒有辦法只用一個命令來做到這一點?

謝謝 :)

創建一個all remote,其中包含多個 repo URL 為其名稱:

git remote add all origin-host:path/proj.git
git remote set-url --add all nodester-host:path/proj.git
git remote set-url --add all duostack-host:path/proj.git

然后只需git push all --all


這是它在.git/config樣子:

  [remote "all"]
  url = origin-host:path/proj.git
  url = nodester-host:path/proj.git
  url = duostack-host:path/proj.git

將所有分支推送到所有遙控器:

git remote | xargs -L1 git push --all

或者,如果您想將特定分支推送到所有遙控器:

master替換為您要推送的分支。

git remote | xargs -L1 -I R git push R master

(獎勵)為命令創建一個 git 別名:

git config --global alias.pushall '!git remote | xargs -L1 git push --all'

運行git pushall現在會將所有分支推送到所有遙控器。

如果您想始終推送到 repo1、repo2 和 repo3,但始終只從 repo1 拉取,請將遠程“原點”設置為

[remote "origin"]
    url = https://exampleuser@example.com/path/to/repo1
    pushurl = https://exampleuser@example.com/path/to/repo1
    pushurl = https://exampleuser@example.com/path/to/repo2
    pushurl = https://exampleuser@example.com/path/to/repo3
    fetch = +refs/heads/*:refs/remotes/origin/*

在命令行配置:

$ git remote add origin https://exampleuser@example.com/path/to/repo1
$ git remote set-url --push --add origin https://exampleuser@example.com/path/to/repo1
$ git remote set-url --push --add origin https://exampleuser@example.com/path/to/repo2
$ git remote set-url --push --add origin https://exampleuser@example.com/path/to/repo3

如果你只想從拉repo1但推repo1repo2特定分支specialBranch

[remote "origin"]
    url = ssh://git@aaa.xxx.com:7999/yyy/repo1.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    ...
[remote "specialRemote"]
    url = ssh://git@aaa.xxx.com:7999/yyy/repo1.git
    pushurl = ssh://git@aaa.xxx.com:7999/yyy/repo1.git
    pushurl = ssh://git@aaa.xxx.com:7999/yyy/repo2.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    ...
[branch "specialBranch"]
    remote = origin
    pushRemote = specialRemote
    ...

請參閱https://git-scm.com/docs/git-config#git-config-branchltnamegtremote

作為編輯 .git/config 文件的 CLI 替代方法,您可以使用以下命令:

# git remote add all origin-host:path/proj.git
# git remote set-url --add all nodester-host:path/proj.git
# git remote set-url --add all duostack-host:path/proj.git

相同的git push all --all在這里也有效。

您已完成與答案 #1 相同的操作。 您剛剛使用命令行完成了它,而不是對配置文件進行原始編輯。

我編寫了一個簡短的 bash 函數,可以在一次調用中推送到多個遙控器。 您可以指定單個遙控器作為參數,多個遙控器以空格分隔,或者不指定任何遙控器以將其推送到所有遙控器。

這可以添加到您的 .bashrc 或 .bash_profile。

function GitPush {
  REMOTES=$@

  # If no remotes were passed in, push to all remotes.
  if [[ -z "$REMOTES" ]]; then
    REM=`git remote`

    # Break the remotes into an array
    REMOTES=$(echo $REM | tr " " "\n")
  fi

  # Iterate through the array, pushing to each remote
  for R in $REMOTES; do
    echo "Pushing to $R..."
    git push $R
  done
}

示例:假設您的存儲庫有 3 個遙控器:rem1、rem2 和 rem3。

# Pushes to rem1
GitPush rem1

# Pushes to rem1 and rem2
GitPush rem1 rem2

# Pushes to rem1, rem2 and rem3
GitPush

您可以使用git hooks - 特別是pre-push :將非源推送添加到.git/hooks/pre-push

如果您已經有一個以origin作為默認遠程的現有存儲庫,並且想要通過調用git push (或您的 IDE / 文本編輯器 git sync 按鈕)推送到多個遠程存儲庫,請首先添加當前推送 URL 作為遠程源推送 URL 。 ..

git remote set-url --push --add origin "$(git remote get-url --push origin)"

...然后對於每個其他遠程存儲庫:

git remote set-url --push --add origin "git@github.com:username/repo-name.git"

現在所有的 fetch 和 pull 操作都只會從你的原始遠程存儲庫中獲取。
但是您可以使用以下命令更新所有遠程存儲庫:

git push

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM