Git: Gitのリモートリポジトリ管理に関する詳細ガイド

リモートリポジトリは、チームでの共同作業の基盤となります。リモートリポジトリを通じて、チームメンバーはコードを共有し、変更内容を同期させ、開発で協力し合うことができます。Gitは複数のリモートリポジトリをサポートしており、さまざまな共同作業モデルを柔軟に設定することが可能です。

1. リモートリポジトリの基礎

(1) リモートリポジトリとは何ですか?

リモートリポジトリとは、Webサーバー上でホストされるバージョン管理リポジトリのことで、以下の目的で使用されます:

100%
graph TB
    subgraph Local
        A[DeveloperALocal Warehouse]
        B[DeveloperBLocal Warehouse]
        C[DeveloperCLocal Warehouse]
    end
    
    subgraph Remote
        D[Remote Repository<br/>GitHub/GitLab]
    end
    
    A <-->|push/pull| D
    B <-->|push/pull| D
    C <-->|push/pull| D
    
    style D fill:#d4edda

(2) リモートリポジトリの種類

一般的なリモートリポジトリホスティングプラットフォーム:

(3) リモートリポジトリ名

デフォルトでは、Git は 2 つの名前を使用します:



2. リモートリポジトリの表示

(1) リモートリポジトリの名前を確認する

▶ サンプル:リモートリポジトリの一覧表示

BASH
# View the name of the remote repository
git remote

# Output:
# origin

# View All Remote Repositories(IncludingURL)
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)

(2) リモートリポジトリの詳細を表示する

▶ サンプル:詳細を表示

BASH
# VieworiginDetails
git remote show origin

# Output:
# * remote origin
#   Fetch URL: https://github.com/user/repo.git
#   Push  URL: https://github.com/user/repo.git
#   HEAD branch: main
#   Remote branches:
#     main     tracked
#     develop  tracked
#     feature  new (next fetch will store in remotes/origin)
#   Local branches configured for 'git pull':
#     main     merges with remote main
#     develop  merges with remote develop
#   Local refs configured for 'git push':
#     main     pushes to main     (up to date)
#     develop  pushes to develop  (local out of date)

(3) リモートブランチの表示

▶ サンプル:リモートブランチの一覧表示

BASH
# View Remote Branches
git branch -r

# Output:
#   origin/HEAD -> origin/main
#   origin/main
#   origin/develop
#   origin/feature

# View All Branches(Local+Remote)
git branch -a

# Output:
# * main
#   develop
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/develop
#   remotes/origin/feature


3. リモートリポジトリを追加する

(1) 新しいリモートリポジトリを追加する

▶ サンプル:リモートリポジトリの追加

BASH
# Add a Remote Repository
git remote add origin https://github.com/user/repo.git

# Add a second remote repository
git remote add upstream https://github.com/original/repo.git

# Add a named remote repository
git remote add backup git@backup-server.com:repo.git

# Verification: Added successfully
git remote -v

# Output:
# origin    https://github.com/user/repo.git (fetch)
# origin    https://github.com/user/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)
# backup    git@backup-server.com:repo.git (fetch)
# backup    git@backup-server.com:repo.git (push)

(2) 異なるフェッチURLとプッシュURLの使用

▶ サンプル:異なるURLの設定

BASH
# Add a Remote Repository: different URLs for fetch and push
git remote add origin https://github.com/user/repo.git
git remote set-url --push origin git@github.com:user/repo.git

# View Configuration
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  git@github.com:user/repo.git (push)

# Common config: fetch via HTTPS, push via SSH

(3) クローン作成時に「origin」を自動的に追加する

▶ サンプル:リポジトリのクローン作成

BASH
# Automatically add when cloning a repositoryorigin
git clone https://github.com/user/repo.git

# View Remote Repositories
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

# Specify a different name when cloning
git clone -o myremote https://github.com/user/repo.git

git remote -v

# Output:
# myremote  https://github.com/user/repo.git (fetch)
# myremote  https://github.com/user/repo.git (push)


4. リモートリポジトリの変更

(1) リモートリポジトリの名前変更

▶ サンプル:名前の変更

BASH
# Rename a Remote Repository
git remote rename origin github

# Verification
git remote -v

# Output:
# github  https://github.com/user/repo.git (fetch)
# github  https://github.com/user/repo.git (push)

(2) リモートリポジトリのURLを変更する

▶ サンプル:URLの変更

BASH
# Modify a Remote RepositoryURL
git remote set-url origin https://github.com/newuser/newrepo.git

# Editfetch URL
git remote set-url --delete origin https://github.com/user/repo.git
git remote set-url --add origin https://github.com/newuser/newrepo.git

# Editpush URL
git remote set-url --push origin git@github.com:user/repo.git

# Verify Changes
git remote -v

(3) URLの追加と削除

▶ サンプル:複数のURLの管理

BASH
# Addfetch URL
git remote set-url --add origin https://backup.com/repo.git

# Addpush URL
git remote set-url --add --push origin https://backup.com/repo.git

# DeleteURL
git remote set-url --delete origin https://backup.com/repo.git

# View AllURL
git remote -v


5. リモートリポジトリの削除

(1) リモートリポジトリへの参照を削除する

▶ サンプル:リモートリポジトリの削除

BASH
# Delete a remote repository reference(Does not affect the remote repository itself)
git remote remove upstream

# Or userm
git remote rm backup

# Confirm Deletion
git remote

# Output:
# origin

(2) 期限切れのリモートブランチへの参照をクリーンアップする

▶ サンプル:リモートブランチのクリーンアップ

BASH
# Delete a remote branch(Branches on a remote repository)
git push origin --delete feature

# Clean up references to remote branches that have been deleted locally
git fetch -p

# Or
git remote prune origin

# View the branches that need to be cleaned up
git remote prune origin --dry-run

# Output:
# * origin/feature would be pruned


6. リモートリポジトリに対する高度な操作

(1) フォークのワークフロー設定

▶ サンプル:フォークワークフローの設定

BASH
# 1. ForkFrom the original repository to your account

# 2. Clone YourselfFork
git clone https://github.com/yourname/repo.git

# 3. Add the original repository asupstream
git remote add upstream https://github.com/original/repo.git

# 4. View Remote Repositories
git remote -v

# Output:
# origin    https://github.com/yourname/repo.git (fetch)
# origin    https://github.com/yourname/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)

# 5. Synchronize with upstream updates
git fetch upstream
git merge upstream/main

# 6. Push to your ownFork
git push origin main

(2) 複数のリモートリポジトリのワークフロー

100%
graph TB
    A[Local Warehouse] -->|push| B[GitHub<br/>origin]
    A -->|push| C[GitLab<br/>gitlab]
    A -->|push| D[Backup Server<br/>backup]
    
    B -->|pull| A
    C -->|pull| A
    
    style A fill:#fff3cd
    style B fill:#d4edda
    style C fill:#c3e6cb
    style D fill:#cce5ff

▶ サンプル:複数のリモートリポジトリの設定

BASH
# Add Multiple Remote Repositories
git remote add origin https://github.com/user/repo.git
git remote add gitlab https://gitlab.com/user/repo.git
git remote add backup git@backup-server.com:repo.git

# Push to all remote repositories
git push origin main
git push gitlab main
git push backup main

# Or addremote.pushDefaultLayout
git config remote.pushDefault origin

# AddpushurlAutomatically Push to Multiple Repositories
git remote set-url --add --push origin https://github.com/user/repo.git
git remote set-url --add --push origin https://gitlab.com/user/repo.git

# Nowpush originIt will be pushed to both repositories at the same time
git push origin main

(3) リモートリポジトリの更新内容を表示する

▶ サンプル:リモート更新の確認

BASH
# Get Remote Updates(Do not merge)
git fetch origin

# View the latest status of a remote branch
git branch -r

# View RemotemainBranch Commits
git log origin/main

# Compare the differences between local and remote
git diff main origin/main

# View details of a remote branch
git show origin/feature


7. リモートリポジトリの設定

(1) リモートリポジトリの設定を行う

▶ サンプル:設定パラメータ

BASH
# View Remote Repository Configuration
git config --local --list | grep remote

# Output:
# remote.origin.url=https://github.com/user/repo.git
# remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

# LayoutfetchStrategy
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

# Layoutpush URL
git config remote.origin.pushurl git@github.com:user/repo.git

# Configure the default push branch
git config branch.main.remote origin
git config branch.main.merge refs/heads/main

(2) SSH 対 HTTPS

▶ サンプル:プロトコルの切り替え

BASH
# UsageHTTPS
git remote set-url origin https://github.com/user/repo.git

# UsageSSH(Recommendations)
git remote set-url origin git@github.com:user/repo.git

# UsageGitAgreement
git remote set-url origin git://github.com/user/repo.git

# HTTPSYou must enter your password each time,Or use credential storage
git config --global credential.helper store

# SSHA key needs to be configured
ssh-keygen -t ed25519 -C "your_email@example.com"
# Add the public key toGitHub/GitLab

❓ よくある質問

Q 「オリジン」と「アップストリーム」の違いは何ですか?
A 「origin」はフォークしたリポジトリ、「upstream」は元のリポジトリのことです。フォークワークフローでは、「upstream」から更新情報をプルし、「origin」にプッシュしてから、プルリクエストを作成します。
Q 複数のリモートリポジトリに同時にプッシュするにはどうすればよいですか?
A git remote set-url --add --push を使用すると、複数のプッシュ用 URL を追加でき、設定されたすべての URL に対して 1 回のプッシュで送信されます。あるいは、各リモートリポジトリに手動でプッシュすることも可能です。
Q リモートリポジトリへの参照を削除すると、そのリモートリポジトリに影響はありますか?
A いいえ。git remote remove これはローカルの参照設定を削除するだけであり、リモートリポジトリ自体には影響しません。リモートリポジトリは引き続き存在しますが、ローカルでは追跡されなくなるだけです。
Q HTTPSとSSH、どちらが良いですか?
A SSHの方が便利です。一度鍵を設定しておけば、毎回パスワードを入力する必要がありません。HTTPSはより簡単で、鍵の設定は必要ありませんが、パスワードを入力するか、認証情報ストアを使用する必要があります。SSHの使用をお勧めします。
Q リモートリポジトリの詳細を確認するにはどうすればよいですか?
A git remote show <name> を使用すると、URL、追跡中のブランチ、設定済みのプル/プッシュルールなど、詳細情報を確認できます。

📖 まとめ


📝 練習問題

  1. 基本演習:ローカルリポジトリを作成し、GitHubのリモートリポジトリを追加し、リモートリポジトリの情報を確認し、リモートリポジトリのURLを変更し、リモートリポジトリの基本的な管理操作を練習します。

  2. 応用演習:フォークワークフローをシミュレートします。originupstream の 2 つのリモートリポジトリを追加し、upstream から更新情報をプルし、それらを origin にプッシュして、フォークワークフローのコラボレーションモデルを理解します。

  3. 課題:複数のリモートリポジトリへの自動プッシュを設定する:1つのリモートリポジトリに対して複数のプッシュURLを設定し、1回のプッシュでGitHub、GitLab、およびバックアップサーバーに同時に送信できるようにする。

Web-Tutorial.com

Web-Tutorial 技術チーム

複数の開発者によって共同維持されているプログラミングチュートリアルプラットフォーム。各チュートリアルは専門分野の開発者が執筆・レビューしています。正確で信頼性の高いコンテンツを目指しています — 問題を見つけた場合はお知らせください。

100%