{"meta":{"title":"リモートリポジトリを管理する","intro":"お使いのコンピューター上のローカル リポジトリと、GitHub 上でホストされているリモート リポジトリを操作する方法について説明します。","product":"概要","breadcrumbs":[{"href":"/ja/get-started","title":"概要"},{"href":"/ja/get-started/git-basics","title":"Git の基礎"},{"href":"/ja/get-started/git-basics/managing-remote-repositories","title":"リモート リポジトリを管理する"}],"documentType":"article"},"body":"# リモートリポジトリを管理する\n\nお使いのコンピューター上のローカル リポジトリと、GitHub 上でホストされているリモート リポジトリを操作する方法について説明します。\n\nこれらのステップを実行してください：\n\n## リモート リポジトリの追加\n\n新しいリモートを追加するには、リポジトリが保存されているディレクトリでターミナルから `git remote add` コマンドを使います。\n\n`git remote add` コマンドは 2 つの引数を取ります。\n\n* リモート名 (例: `origin`)\n* リモート URL (例: `https://github.com/OWNER/REPOSITORY.git`)\n\n次に例を示します。\n\n```shell\n$ git remote add origin https://github.com/OWNER/REPOSITORY.git\n# Set a new remote\n\n$ git remote -v\n# Verify new remote\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n```\n\nどの URL を使うかの詳細については、「[リモートリポジトリについて](/ja/get-started/git-basics/about-remote-repositories)」を参照してください。\n\n### トラブルシューティング: リモートオリジンが既に存在します\n\nこのエラーは、ローカルのリポジトリに既に存在している名前でリモートを追加しようとしたということです。\n\n```shell\n$ git remote add origin https://github.com/octocat/Spoon-Knife.git\n> fatal: remote origin already exists.\n```\n\nこの問題を解決するには、以下を実行してください。\n\n* 新しいリモートに別の名前を使う。\n* 新しいリモートを追加する前に、既存のリモート リポジトリの名前を変更する。 詳細については、後述する「[リモート リポジトリの名前を変更する](#renaming-a-remote-repository)」を参照してください。\n* 新しいリモートを追加する前に、既存のリモート リポジトリの名前を削除する。 詳細については、後述する「[リモート リポジトリの削除](#removing-a-remote-repository)」を参照してください。\n\n## リモート リポジトリの URL の変更\n\n`git remote set-url` コマンドは、既存のリモート リポジトリ URL を変更します。\n\n> \\[!TIP]\n> HTTPS と SSH の URL の違いについては、「[リモートリポジトリについて](/ja/get-started/git-basics/about-remote-repositories)」を参照してください。\n\n`git remote set-url` コマンドは 2 つの引数を取ります。\n\n* 既存のリモート名。 たとえば、`origin` や `upstream` の 2 つが一般的な選択肢です。\n* リモートの新しい URL。 次に例を示します。\n\n  * HTTPS を使うよう更新する場合、URL は以下のようになります:\n\n  ```shell\n  https://github.com/OWNER/REPOSITORY.git\n  ```\n\n  * SSH を使うよう更新する場合、URL は以下のようになります:\n\n  ```shell\n  git@github.com:OWNER/REPOSITORY.git\n  ```\n\n### リモート URL の SSH から HTTPS への切り替え\n\n1. <span class=\"platform-mac\">\\[ターミナル]</span><span class=\"platform-linux\">\\[ターミナル]</span><span class=\"platform-windows\">\\[Git Bash]</span> を開きます。\n\n2. ワーキングディレクトリをローカルプロジェクトに変更します。\n\n3. 変更したいリモートの名前を取得するため、既存のリモート一覧を表示します。\n\n   ```shell\n   $ git remote -v\n   > origin  git@github.com:OWNER/REPOSITORY.git (fetch)\n   > origin  git@github.com:OWNER/REPOSITORY.git (push)\n   ```\n\n4. `git remote set-url` コマンドを使用して、リモートの URL を SSH から HTTPS に変更します。\n\n   ```shell\n   git remote set-url origin https://github.com/OWNER/REPOSITORY.git\n   ```\n\n5. リモート URL が変更されたことを検証します。\n\n   ```shell\n   $ git remote -v\n   # Verify new remote URL\n   > origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n   > origin  https://github.com/OWNER/REPOSITORY.git (push)\n   ```\n\n次回、リモート リポジトリに対して c0 />、<c1 />、または <c2 /> を<すると、GitHubのユーザー名とパスワードを求められます。 Git からパスワードの入力を求められたら、 personal access tokenを入力します。 または、[Git Credential Manager](https://github.com/GitCredentialManager/git-credential-manager/blob/main/README.md) などの認証情報ヘルパーを使用できます。 より安全な認証方法を優先し、Git のパスワードベースの認証が削除されました。 詳しくは、「[個人用アクセス トークンを管理する](/ja/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)」をご覧ください。\n\n[資格情報ヘルパーを使って](/ja/get-started/git-basics/caching-your-github-credentials-in-git)、GitがGitHubと通信するたびに、あなたのGitHubユーザー名とpersonal access tokenを覚えるようにすることができます。\n\n### リモート URL の HTTPS から SSH への切り替え\n\n1. <span class=\"platform-mac\">\\[ターミナル]</span><span class=\"platform-linux\">\\[ターミナル]</span><span class=\"platform-windows\">\\[Git Bash]</span> を開きます。\n\n2. ワーキングディレクトリをローカルプロジェクトに変更します。\n\n3. 変更したいリモートの名前を取得するため、既存のリモート一覧を表示します。\n\n   ```shell\n   $ git remote -v\n   > origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n   > origin  https://github.com/OWNER/REPOSITORY.git (push)\n   ```\n\n4. `git remote set-url` コマンドを使用して、リモートの URL を HTTPS から SSH に変更します。\n\n   ```shell\n   git remote set-url origin git@github.com:OWNER/REPOSITORY.git\n   ```\n\n5. リモート URL が変更されたことを検証します。\n\n   ```shell\n   $ git remote -v\n   # Verify new remote URL\n   > origin  git@github.com:OWNER/REPOSITORY.git (fetch)\n   > origin  git@github.com:OWNER/REPOSITORY.git (push)\n   ```\n\n### トラブルシューティング: このようなリモート '\\[name]' はありません\n\nこのエラーは、変更しようとしたリモートが存在しないことを意味します。\n\n```shell\n$ git remote set-url sofake https://github.com/octocat/Spoon-Knife\n> fatal: No such remote 'sofake'\n```\n\nリモート名を正しく入力したか確認してください。\n\n## リモート リポジトリの名前を変更する\n\n`git remote rename` コマンドを使用して、既存のリモートの名前を変更します。\n\n`git remote rename` コマンドは 2 つの引数を取ります。\n\n* 既存のリモート名 (例: `origin`)\n* リモートの新しい名前 (例: `destination`)\n\n### リモート リポジトリの名前変更の例\n\nこれらの例では、[HTTPS を使用して複製](/ja/get-started/git-basics/about-remote-repositories#cloning-with-https-urls)することを前提としています (推奨)。\n\n```shell\n$ git remote -v\n# View existing remotes\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n\n$ git remote rename origin destination\n# Change remote name from 'origin' to 'destination'\n\n$ git remote -v\n# Verify remote's new name\n> destination  https://github.com/OWNER/REPOSITORY.git (fetch)\n> destination  https://github.com/OWNER/REPOSITORY.git (push)\n```\n\n### トラブルシューティング: 構成セクションの 'remote.\\[古い名前]' を 'remote.\\[新しい名前]' に変更できませんでした\n\nこのエラーは、入力した古いリモート名が存在しないことを意味します。\n\n`git remote -v` コマンドを使用すると、現在存在するリモートを確認できます。\n\n```shell\n$ git remote -v\n# View existing remotes\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n```\n\n### トラブルシューティング: リモート \\[新しい名前] は既に存在します\n\nこのエラーは、使用しようとしたリモート名がすでに存在する、という意味です。 これを解決するには、別のリモート名を使用するか、または元のリモートの名前を変更するかのいずれかです。\n\n## リモート リポジトリを削除する\n\n`git remote rm` コマンドを使用して、リポジトリからリモート URL を削除します。\n\n`git remote rm` コマンドは、次の 1 つの引数を受け取ります。\n\n* リモート名 (例: `destination`)\n\nリポジトリからリモート URL を削除すると、ローカルおよびリモート リポジトリのリンクのみが解除されます。 リモート リポジトリは削除されません。\n\n### リモート リポジトリの削除の例\n\nこれらの例では、[HTTPS を使用して複製](/ja/get-started/git-basics/about-remote-repositories#cloning-with-https-urls)することを前提としています (推奨)。\n\n```shell\n$ git remote -v\n# View current remotes\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n> destination  https://github.com/FORKER/REPOSITORY.git (fetch)\n> destination  https://github.com/FORKER/REPOSITORY.git (push)\n\n$ git remote rm destination\n# Remove remote\n$ git remote -v\n# Verify it's gone\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n```\n\n> \\[!NOTE]\n> `git remote rm` では、サーバーからリモート リポジトリは削除されません。 リモートとその参照がローカル リポジトリから削除されるだけです。\n\n### トラブルシューティング: 構成セクション 'remote.\\[名前]' を削除できませんでした。\n\nこのエラーは、削除しようとしたリモートが存在しないことを意味します。\n\n```shell\n$ git remote rm sofake\n> error: Could not remove config section 'remote.sofake'\n```\n\nリモート名を正しく入力したか確認してください。\n\n## 参考資料\n\n* [\n  *Pro Git* ブックの「リモートでの作業」](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes)"}