このセクションで学ぶこと
「リポジトリ」はGitの最も重要な概念です。リポジトリとは何か、どんな種類があるかを学びましょう。
リポジトリ(Repository)とは
一言で言うと
ファイルの変更履歴を保存する「箱」
例え話
リポジトリは「タイムカプセル付きの倉庫」のようなもの。
- ファイルを入れると保管される
- いつ何を入れたか全部記録される
- 過去に入れたものも取り出せる
略称
「リポジトリ」は長いので、よく「リポ」や「repo(レポ)」と略されます。
ローカルリポジトリとリモートリポジトリ
Gitには2種類のリポジトリがあります。
graph TD
subgraph RemoteBox["リモートリポジトリ(GitHub, GitLabなど)"]
Remote["みんなで共有する場所"]
end
subgraph LocalBox["ローカルリポジトリ(あなたのPC)"]
Local["自分だけの作業場所"]
end
Local -->|"push"| Remote
Remote -->|"pull / clone"| Local
style RemoteBox fill:#dbeafe,stroke:#2563eb,stroke-width:2px,color:#1e40af
style LocalBox fill:#d1fae5,stroke:#059669,stroke-width:2px,color:#064e3b
style Remote fill:#e0e7ff,stroke:#4f46e5,color:#312e81
style Local fill:#f0fdf4,stroke:#16a34a,color:#14532d
ローカルリポジトリ
一言で言うと
あなたのPC上にあるリポジトリ
特徴
| 項目 | 内容 |
|---|---|
| 場所 | 自分のPC |
| アクセス | 自分だけ |
| ネット接続 | 不要 |
| 用途 | 日常の作業 |
例
graph TD
MyApp["my-app/"]
Git[".git/ ← リポジトリの本体(隠しフォルダ)"]
Index["index.html"]
Style["style.css"]
Script["script.js"]
MyApp --> Git
MyApp --> Index
MyApp --> Style
MyApp --> Script
style MyApp fill:#d1fae5,stroke:#059669,stroke-width:2px,color:#064e3b
style Git fill:#fef3c7,stroke:#d97706,stroke-width:2px,color:#92400e
style Index fill:#f3f4f6,stroke:#9ca3af,color:#374151
style Style fill:#f3f4f6,stroke:#9ca3af,color:#374151
style Script fill:#f3f4f6,stroke:#9ca3af,color:#374151
.git フォルダの中に、すべての変更履歴が保存されています。
リモートリポジトリ
一言で言うと
インターネット上にあるリポジトリ
特徴
| 項目 | 内容 |
|---|---|
| 場所 | GitHub, GitLabなどのサーバー |
| アクセス | 設定次第(公開 or プライベート) |
| ネット接続 | 必要 |
| 用途 | バックアップ、チーム共有 |
例(GitHub)
https://github.com/tanaka/my-app
ブラウザでアクセスして、コードを見たりダウンロードしたりできます。
ローカルとリモートの関係
基本的な流れ
1. リモートからコピー(clone)
GitHub → 自分のPC
2. ローカルで作業
ファイルを編集 → commit
3. リモートに送信(push)
自分のPC → GitHub
4. リモートから取得(pull)
GitHub → 自分のPC(他の人の変更を取り込む)
コマンドとの対応
| 操作 | コマンド | 方向 |
|---|---|---|
| コピー | git clone | リモート → ローカル |
| 送信 | git push | ローカル → リモート |
| 取得 | git pull | リモート → ローカル |
リポジトリの作り方(2つの方法)
方法1: ゼロから作る(git init)
mkdir my-project # フォルダを作成
cd my-project # フォルダに移動
git init # リポジトリを初期化
→ 空のローカルリポジトリができる
方法2: リモートからコピー(git clone)
git clone https://github.com/someone/some-project.git
→ リモートリポジトリの完全なコピーがローカルにできる
.gitフォルダの中身
リポジトリを作ると、.git という隠しフォルダができます。
ls -la .git
graph TD
GitDir[".git/"]
HEAD["HEAD - 現在のブランチ"]
Config["config - リポジトリの設定"]
Hooks["hooks/ - 自動実行スクリプト"]
Objects["objects/ - ファイルの実体(圧縮)"]
Refs["refs/ - ブランチやタグの情報"]
GitDir --> HEAD
GitDir --> Config
GitDir --> Hooks
GitDir --> Objects
GitDir --> Refs
style GitDir fill:#fef3c7,stroke:#d97706,stroke-width:2px,color:#92400e
style HEAD fill:#f3f4f6,stroke:#9ca3af,color:#374151
style Config fill:#f3f4f6,stroke:#9ca3af,color:#374151
style Hooks fill:#f3f4f6,stroke:#9ca3af,color:#374151
style Objects fill:#f3f4f6,stroke:#9ca3af,color:#374151
style Refs fill:#f3f4f6,stroke:#9ca3af,color:#374151
中身を覚える必要はありません。「.gitフォルダ = リポジトリの本体」と覚えておけばOK。
注意
.git
フォルダを削除すると、すべての履歴が消えます。間違って消さないように注意しましょう。
公開リポジトリと非公開リポジトリ
GitHub/GitLabでリポジトリを作るとき、公開設定を選べます。
| 種類 | 誰が見れる | 用途 |
|---|---|---|
| Public(公開) | 誰でも | オープンソース、ポートフォリオ |
| Private(非公開) | 招待された人だけ | 業務、個人プロジェクト |
GitHubの場合
- 無料アカウントでもPrivateリポジトリは無制限に作れる
- 企業の業務コードは通常Private
ハンズオン
実際にローカルリポジトリを作ってみましょう。
1. 練習用フォルダを作成
mkdir ~/git-practice
cd ~/git-practice
2. リポジトリを初期化
git init
出力例:
Initialized empty Git repository in /Users/tanaka/git-practice/.git/
3. .gitフォルダを確認
ls -la
出力例:
total 0
drwxr-xr-x 3 tanaka staff 96 1 27 10:00 .
drwxr-xr-x+ 28 tanaka staff 896 1 27 10:00 ..
drwxr-xr-x 9 tanaka staff 288 1 27 10:00 .git
→ .git フォルダが作成されました!
4. Gitの状態を確認
git status
出力例:
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
→ 空のリポジトリが完成しました!
まとめ
| 用語 | 意味 |
|---|---|
| リポジトリ | 変更履歴を保存する箱 |
| ローカルリポジトリ | 自分のPC上のリポジトリ |
| リモートリポジトリ | インターネット上のリポジトリ |
| .gitフォルダ | リポジトリの本体 |
チェックリスト
- リポジトリとは何か説明できる
- ローカルとリモートの違いが分かる
-
git initでリポジトリを作成できる
次のステップへ
リポジトリの概念は理解できましたか?
次のセクションでは、Gitでよく使う基本用語を学びます。commit, add, push など、最初は混乱しやすい言葉を整理しましょう。
推定読了時間: 25分