タイトル: リモートリポジトリの作成
SEOタイトル: git リモートリポジトリ作成完全ガイド — GitHub/GitLab/自前 bare
| この記事の要点 |
|
リモートリポジトリとは
ローカル PC で作ったリポジトリは、そのままでは自分しか使えません。リモートリポジトリ(共有サーバ側のリポジトリ)を作って git push することで、複数人で共有 / バックアップ / CI 連携が可能になります。
代表的なホスティング:
| サービス | 特徴 |
|---|---|
| GitHub | 世界最大、OSS の集積地、Actions / Codespaces 統合 |
| GitLab | セルフホスト可、CI/CD が標準装備 |
| Bitbucket | Atlassian。Jira/Confluence 連携 |
| AWS CodeCommit | AWS 内に閉じた運用 |
| Azure Repos | Azure DevOps の一部 |
| Gitea / Gogs | 軽量 OSS セルフホスト |
自前 git init --bare | SSH サーバさえあれば構築可 |
GitHub での作成手順
- 右上の + → New repository
- Repository name:
myapp - Description: 説明(任意)
- Public / Private: 公開範囲
- Add a README file: チェックすると即座にコミット 1 件入る
- Add .gitignore: 言語を選択(Node / Python / Laravel など)
- Choose a license: MIT / Apache-2.0 / GPL-3.0 など
- Create repository
作成後、URL を取得します:
# SSH (推奨)
git@github.com:user/myapp.git
# HTTPS (CI 等)
https://github.com/user/myapp.git
ローカルからリモートへの紐付け
# ケース A: ローカル既存 → 空リモートに push
cd ~/projects/myapp
git remote add origin git@github.com:user/myapp.git
git branch -M main # ブランチ名 main に統一
git push -u origin main
# ケース B: README 等を入れて作った GitHub リポジトリを clone
git clone git@github.com:user/myapp.git
cd myapp
# ケース C: ローカルとリモートに別履歴があり一発で繋ぎたい
git remote add origin git@github.com:user/myapp.git
git pull origin main --allow-unrelated-histories
# コンフリクト解決 → commit → push
git push -u origin main
自前サーバに bare リポジトリで構築
SSH ログイン可能なサーバがあれば、git init --bare だけで Git リモートを立てられます。費用ゼロ、社内専用に最適。
# サーバ側 (例: git ユーザーで運用)
sudo useradd -m -s /bin/bash git
sudo su - git
mkdir -p /home/git/repos/myapp.git
cd /home/git/repos/myapp.git
git init --bare
# Initialized empty Git repository in /home/git/repos/myapp.git/
# 公開鍵を /home/git/.ssh/authorized_keys に追加
# (複数開発者で共有)
# クライアント側
git remote add origin git@server.example.com:/home/git/repos/myapp.git
git push -u origin main
HTTP / smart-http で公開
# Apache + git-http-backend で HTTP push/pull 可能
# /etc/apache2/conf-available/git.conf
SetEnv GIT_PROJECT_ROOT /var/git/repos
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
<Location /git>
AuthType Basic
AuthName "Git"
AuthUserFile /etc/apache2/git.passwd
Require valid-user
</Location>
# クライアント
git clone https://server.example.com/git/myapp.git
README / LICENSE / .gitignore
# 既存リポジトリにあとから足す場合
cat > README.md <<'EOF'
# myapp
## Setup
1. composer install
2. cp .env.example .env
3. php artisan key:generate
## Test
- php artisan test
EOF
# MIT ライセンス
curl https://raw.githubusercontent.com/licenses/license-templates/master/templates/mit.txt > LICENSE
# .gitignore (言語別テンプレートから)
curl https://raw.githubusercontent.com/github/gitignore/main/Laravel.gitignore > .gitignore
git add README.md LICENSE .gitignore
git commit -m "Add README, LICENSE, .gitignore"
git push
Organization での運用
GitHub Org の典型構成:
myorg/ Organization
├── frontend リポジトリ
├── backend
├── docs
└── infra
メンバー権限:
- Owner : Org 全体管理
- Member : 通常メンバー
- Outside : 外部協力者(特定リポジトリのみ)
Team:
- @myorg/dev : 開発者全員(Write 権限を全リポジトリに)
- @myorg/ops : 運用(infra リポジトリ Admin)
- @myorg/reviewers : レビューワー(プルリク CODEOWNERS で自動アサイン)
SSH / HTTPS の切り替え
# 現状確認
git remote -v
# origin https://github.com/user/myapp.git (fetch)
# origin https://github.com/user/myapp.git (push)
# HTTPS → SSH に変更
git remote set-url origin git@github.com:user/myapp.git
# SSH → HTTPS に変更
git remote set-url origin https://github.com/user/myapp.git
# fetch と push で別 URL(push は SSH に強制)
git remote set-url --push origin git@github.com:user/myapp.git
# リモート追加・削除
git remote add upstream git@github.com:original/myapp.git
git remote remove upstream
git remote rename origin github
ミラーリポジトリ
GitHub のリポジトリを GitLab にも同期する / バックアップ用 — の場合はミラー pushを使います。
# ベアでクローン
git clone --mirror git@github.com:user/myapp.git
cd myapp.git
# 別リモートを追加
git remote set-url --push origin git@gitlab.com:user/myapp.git
# 全ブランチ・タグ・参照を push
git push --mirror
# 定期同期 (cron で)
*/15 * * * * cd /srv/mirror/myapp.git && git fetch -p origin && git push --mirror gitlab
Private / Public の判断
| 用途 | 推奨 |
|---|---|
| OSS 公開 | Public |
| 業務コード | Private(GitHub Free でも無制限) |
| 勉強用 | Public でも Private でも |
| 機密情報(.env 含む) | 絶対 Private(できれば履歴に含めない) |
FAQ
Q: 間違って .env を push してしまった
A: 履歴から削除 (git filter-repo) + 必ずシークレットを無効化 (API キー再発行)。push 済の時点で公開済と考えるべき。
Q: 既に main と master の 2 ブランチがある
A: git push origin main 後 GitHub 設定で Default branch を main に変更 → git push origin :master で削除。
Q: 自前 Git サーバの認証は SSH 鍵で全部済ませたい
A: git ユーザの ~/.ssh/authorized_keys にメンバー全員の公開鍵を追加。Gitolite / Gitea を使うと権限管理が楽。