[GitHub] Fork したリポジトリを本家リポジトリと同期する方法
GitHub で Fork (フォーク) した Repository (リポジトリ) を本家リポジトリに追従する手順をご紹介します。
前提
GitHub 上で既に何かしらの Repository を Fork している前提とします。
Fork した repository を git clone
まず、Fork した repository を git clone します。
git clone git@github.com:your_account/sample_repository.git |
Fork 元のリポジトリを upstream というリモートリポジトリ名に設定
リモートリポジトリとして Fork 元のオリジナルリポジトリを upstream という名前で設定します。
git remote add upstream git://github.com/original_account/sample_repository.git |
以後、このリポジトリは upstream という名前で Fork 元のオリジナルリポジトリを扱うことができます。
最初の1回だけ設定すれば、再設定の必要はありません。
git branch -a で upstream リポジトリの確認
$ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/master remotes/upstream/master |
本家リポジトリの変更に追従するために git fetch と merge
まず git fetch upstream
$ git fetch upstream remote: Counting objects: 1, done. remote: Total 1 (delta 0), reused 1 (delta 0) Unpacking objects: 100% (1/1), done. From git://github.com/original_account/sample_repository * [new branch] develop -> upstream/develop * [new branch] master -> upstream/master |
次に git merge
git fetch で取得した差分を current branch へ merge させます。
$ git merge upstream/master Updating 1e579f8..21e70ae Fast-forward |
OSS 開発に馴染みのない方にとっては GitHub で Fork したリポジトリで作業して、本家ブランチと同期をとるということも少ないと思うので、本記事が少しでも役に立てば幸いです。