---
# System prepended metadata

title: 多個 git 遠端 repositories
tags: [CLI, git]

---

# 多個 git 遠端 repositories

[TOC]

###### tags: `git` `repository` `repositories` `remote` `multiple`

---

將一個 local repository <SUB>(本地版本庫)</SUB>**同時**提交到多個 remote repository <SUB>(遠端版本庫)</SUB>。

> NAME
>	- git-remote - Manage the set of repositories ("remotes") whose branches you track.

---

## 檢視

### 檢視已設好的 remote repository <SUB>(遠端版本庫)</SUB>的 shortname(s)<SUB>(簡稱)</SUB>

> DESCRIPTION
> - With no arguments, shows a list of existing remotes. Several subcommands are available to perform operations on the remotes.

```shell
$ git remote
```

### 顯示 remote repository <SUB>(遠端版本庫)</SUB>的網址

> OPTIONS
> - `-v`, `--verbose`
>	- Be a little more verbose and show remote url after name. NOTE: This must be placed between remote and subcommand.

```shell
$ git remote -v
```

### 檢視 remote repository <SUB>(遠端版本庫)</SUB>

> COMMANDS
> - `show`
>	- Gives some information about the remote `name`.

```shell
$ git remote show [<name>]
```

未指定`name`時顯示所有版本庫；指定`name`時則顯示特定版本庫。

## 新增

### 新增一個 remote repository <SUB>(遠端版本庫)</SUB>

> COMMANDS
> - `add`
>	- Adds a remote named `name` for the repository at `url`.

```shell
$ git remote add <name> <url>
```

### 新增一個提交用的 remote repository <SUB>(遠端版本庫)</SUB>

> COMMANDS
> - `set-url`
>	- Changes URLs for the remote.
>	With `--push`, push URLs are manipulated instead of fetch URLs.
>	With `--add`, instead of changing existing URLs, new URL is added.

```shell
$ git remote set-url --add [--push] <name> <newurl>
```

## 推送

### 推送至單一 remote repository <SUB>(遠端版本庫)</SUB>

```shell
$ git push origin
```

`origin` 為 remote repository <SUB>(遠端版本庫)</SUB>的 name <SUB>(名稱)</SUB>。

### 一次推送所有 branches <SUB>(分支)</SUB>至所有 remote repositories <SUB>(遠端版本庫)</SUB>

```shell
$ git remote | xargs -L1 git push --all
```

### 一次推送單一 branch <SUB>(分支)</SUB>至所有 remote repositories <SUB>(遠端版本庫)</SUB>

```shell
$ git remote | xargs -L1 -I R git push R master
```

`master` 為欲推送的分支。

## 指定預設來源

```shell
$ git branch --set-upstream-to origin
```

## 參考資料來源

- Git Manual
man page from **macOS Catalina**<SUP>Version 10.15.5</SUP>
- [Pro Git book, 2nd Edition (2014)](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes)
- [StackOverflow - Able to push to all git remotes with the one command?](https://stackoverflow.com/questions/5785549/able-to-push-to-all-git-remotes-with-the-one-command#answer-5785618)
- [How to set default remote in git?](https://stackoverflow.com/questions/55045596/how-to-set-default-remote-in-git)
- [Changing the Git remote 'push to' default](https://stackoverflow.com/questions/18801147/changing-the-git-remote-push-to-default)
