🐙
[GitHub] fork元のリポジトリをupstreamに設定する
🌱 はじめに
この記事では、fork元のリポジトリを upstream に設定する方法を解説します。
fork したリポジトリには、fork元の更新が自動では反映されません。
fork元の変更を取り込むために、fork元のリポジトリを upstream という名前でリモートに登録します。
-
origin: 自分の fork(clone したリポジトリ) -
upstream: fork元のリポジトリ

upstream は慣習的に使われている名前で、Git の予約語ではありません。
🌱 結論
🌱 1. 状況を確認する
下記のコマンドで状況を確認します。
git remote -v
出力結果を確認する
$ git remote -v
origin https://github.com/user-name/sample-project.git (fetch)
origin https://github.com/user-name/sample-project.git (push)
🌱 2. upstream に設定する
下記のコマンドで、fork元のリポジトリを upstream として登録します。
git remote add upstream https://github.com/company-name/sample-project.git
もう一度、下記のコマンドで状況を確認します。
git remote -v
出力結果を確認する
$ git remote -v
origin https://github.com/user-name/sample-project.git (fetch)
origin https://github.com/user-name/sample-project.git (push)
upstream https://github.com/company-name/sample-project.git (fetch)
upstream https://github.com/company-name/sample-project.git (push)
🌱 3. fork元の変更を取り込む
upstream を登録したら、下記のコマンドで fork元の変更を取り込みます。
「はじめに」の図の ① fetch → ② merge → ③ push の順に実行します(② の前に git switch main で main ブランチに切り替えます)。
デフォルトブランチ名が main 以外(master など)の場合は、読み替えてください。
git fetch upstream
git switch main
git merge upstream/main
git push origin main
Discussion