# フォーク開発したい ```console $ gh repo fork OWNER/REPO ``` 「フォーク」とは第三者のリポジトリを自分のアカウントにコピーする作業です。 これにより、自分のリポジトリを作業スペースとして、元の開発に安全に協力できます。 GitHub上のリポジトリは`gh`コマンドでフォーク&クローンを作成できます。 :::{note} GitLabにもフォーク機能はありますが、 フォークを活用した共同開発は、 どちらかといえばGitHubに根付いている文化のようです。 ::: ## 手動でフォークしたい ```console // GitHub上で「自分がフォークした」リポジトリ $ git clone https://github.com/YOUR_USERNAME/REPOSITORY_NAME.git $ cd REPOSITORY_NAME // 「元のリポジトリ」をupstreamとして登録 $ git remote add upstream https://github.com/ORIGINAL_OWNER/REPOSITORY_NAME.git $ git remote --verbose origin https://github.com/YOUR_USERNAME/project.git (fetch) origin https://github.com/YOUR_USERNAME/project.git (push) upstream https://github.com/ORIGINAL_OWNER/project.git (fetch) upstream https://github.com/ORIGINAL_OWNER/project.git (push) // 事故防止:upstreamにpush禁止 $ git remote set-url --push upstream DISABLED // 設定を確認 $ git remote --verbose origin https://github.com/YOUR_USERNAME/project.git (fetch) origin https://github.com/YOUR_USERNAME/project.git (push) upstream https://github.com/ORIGINAL_OWNER/project.git (fetch) upstream DISABLED ``` ブラウザなどでフォークしたリポジトリを、手動でクローンしてセットアップする手順です。 `upstream`として設定したオリジナルのリポジトリに`git push`できないようにすることで、誤操作を防ぎ、安全に管理できるようにしています。 :::{seealso} - [](./git-remote.md) ::: ## 元のリポジトリの最新状態を取得したい ```console // 作業ブランチをmainに切り替える $ git switch main // upstreamから最新情報を取得する $ git fetch upstream // upstream/main を 自分のmainに取り込む $ git merge upstream/main // 自分のリポジトリのmainを更新する $ git push origin main ``` フォーク開発をしていると、元のリポジトリと、自分のリポジトリの間に差が生じます。 定期的に`upstream`の変更を取り込み、乖離しすぎないように注意しましょう。 :::{note} こまめに作業している場合は、とくに問題にならないかもしれませんが、しばらく放っておくとあっという間に乖離します。 乖離しすぎた場合は、フォークを作り直すのもひとつの選択肢だと思います。 ::: ```console $ git rebase upstream/main ``` 元のリポジトリの変更を取り込むときには `merge`の代わりに`rebase`を使うこともできます。 どちらを使うかはリポジトリごとの管理ポリシーや開発方針を参考にして判断してください。 ## 元のリポジトリへのプッシュを禁止したい ```console $ git remote set-url --push upstream DISABLED // 他に $ git remote set-url --push upstream NO_PUSH $ git remote set-url --push upstream READONLY $ git remote set-url --push upstream FORBIDDEN $ git remote set-url --push upstream DENIED ``` 元のリポジトリ(`upstream`)に間違えてプッシュしないための設定です。 プッシュ用のURLを無効なURLを設定します。 任意の文字列で構いませんが、 `DISABLED`、 `NO_PUSH`、 `READONLY` などがよく使われるようです。 ## ブランチを作成したい ```console $ git switch main $ git pull upstream main $ git switch --create feature/ $ git add $ git commit -m "feat: add new implementation" $ git push --set-upstream origin feature/ ``` ## ブランチを整理したい ```console $ git switch main $ git branch --delete feature/ $ git remote prune origin ``` ## プルリクエストしたい ```console // 新しいブランチを作成して切り替え $ git switch --create feature/ // 自分のフォークにプッシュ $ git push -u origin feature/ // GitHub上で「Compare & Pull Request」 ``` 開発ブランチを **自分のフォーク** にプッシュします。 リモートに開発ブランチがない場合は `git push --set-upstream`します。 リモートブランチが存在する場合は `git push`で大丈夫です。