🍴

Git リポジトリを GitHub に Fork する

2024/03/17に公開

まえがき

詳しくは参考記事を参照のこと。

とあるGitリポジトリを別のGitリポジトリにForkしたい場合の手順のまとめです。
今回の私の場合は個人がホストするGiteaからGitHubへのForkです。

  • Forkもと(upstream)のコピーを維持するブランチ
    • upstream/master
  • Fork後(origin)の改変を維持するブランチ
    • origin/main

参考

手順

  1. 空のGitHubリポジトリを作成(ここにForkを作る)
  2. GitHubリポジトリをローカルにクローン

ghqを使っているので下記で実施

ghq get https://github.com/{user}/{repository}

移動して確認する

git remote -v
origin  https://github.com/{user}/{repository} (fetch)
origin  https://github.com/{user}/{repository} (push)
  1. upstreamを設定(=forkもと。ここからコピーする)
git remote add upstream https://git.example.com/{user}/{repogitory}.git
git remote -v
origin  https://github.com/{user}/{repository} (fetch)
origin  https://github.com/{user}/{repository} (push)
upstream        https://git.example.com/{user}/{repogitory}.git (fetch)
upstream        https://git.example.com/{user}/{repogitory}.git (push)
  1. upstreamをローカルリポジトリに取得
git fetch upstream

masterブランチに切り替える

git switch master

upstreamにmasterブランチがない場合はエラーになるのかも
その場合は下記の感じ?(未検証)

git switch -c master
git merge upstream/master
  1. mainブランチを作成し、デフォルトブランチにする
git switch -c main
git push origin main

最初に作られたブランチのため、デフォルトになる

  1. upstreamもpushする
git switch master
git push origin master

2番目以降に作られたブランチのため、デフォルトにはならない

  1. 開発していく
    mainまたは開発用のブランチで作業する
git switch main

Discussion