2026/06/18

Gitリポジトリの操作

Gitでは変更の保存先が4つの場所に分かれており、コマンドによってどの場所まで変更を反映するかが決まる。
`add` → `commit` → `push` と段階を踏むのが基本で、`commit -a` や `pull` は複数の操作をひとつのコマンドにまとめた省略形にあたる。

---

## 4つの場所

| 場所 | 別名 | 物理的な位置 | 役割 |
|------|------|-------------|------|
| Workspace | 作業ディレクトリ | 自PC(.git/ の外) | 実際にファイルを編集する場所 |
| Index | ステージングエリア | 自PC(.git/ 内) | 次のコミットに含める変更を仮置きする場所 |
| Local repo | ローカルリポジトリ | 自PC(.git/ 内) | コミット履歴・ブランチの保管場所 |
| Remote repo | リモートリポジトリ | 外部サーバー | GitHub / GitLab など、チームで共有する場所 |

---

## 送信系コマンド(ローカル → リモート方向)

### `git add`:Workspace → Index

```bash
git add <ファイル>   # 指定ファイルをステージ
git add .           # 全変更をステージ
```

### `git commit`:Index → Local repo

```bash
git commit -m "メッセージ"
```

### `git commit -a`:Workspace → Local repo(add + commit を一括)

```bash
git commit -a -m "メッセージ"
```

追跡済みファイル(tracked)の `add` と `commit` を一括で実行する省略形。
新規ファイル(untracked)は自動でステージされないため、別途 `git add` が必要。

### `git push`:Local repo → Remote repo

```bash
git push origin main
git push -u origin main   # 初回。-u で upstream を設定し、以後は git push だけで OK
```

---

## 取得系コマンド(リモート → ローカル方向)

### `git fetch`:Remote repo → Local repo

```bash
git fetch origin
```

リモートの更新を Local repo に取り込むだけ。Workspace は変化しない。
取得後に `git log origin/main` で差分を確認してから merge できる。

### `git pull`:Remote repo → Workspace(fetch + merge を一括)

```bash
git pull origin main
```

`fetch` に続いて merge まで一括で行い、Workspace まで反映する。
コンフリクトが起きる可能性があるため、`fetch` → 確認 → `merge` の順が安全。

### `git checkout` / `git switch`:Local repo → Workspace

```bash
git checkout <ブランチ名>   # ブランチを切り替えてワーキングツリーを更新
git switch <ブランチ名>     # 現代的な書き方(checkout の代替)
```

### `git restore`:Local repo → Workspace または Index

```bash
git restore <ファイル>              # Workspace の変更を最終コミット時点に戻す
git restore --staged <ファイル>     # Index の変更を取り消す(Workspace は変化しない)
```

### `git clone`:Remote repo → Workspace(初回のみ)

```bash
git clone 
```

`init` + `fetch` + `checkout` を一括実行し、リポジトリ全体をローカルにコピーする。

---

## 比較系コマンド(差分確認)

| コマンド | 比較する場所 | 確認できること |
|----------|-------------|----------------|
| `git diff` | Workspace ↔ Index | `add` 前の変更(まだステージしていない差分) |
| `git diff HEAD` | Workspace ↔ Local repo | 最終コミットから現在までの全差分 |
| `git diff --staged` | Index ↔ Local repo | `add` 済みでまだ `commit` していない差分 |

---

## リポジトリの作成

### `git init`:空のローカルリポジトリを新規作成する

手元にあるフォルダをGit管理下に置く。Remote repo なしのローカル完結でも運用でき、後から `git remote add` でリモートに接続することもできる。

```bash
mkdir my-project && cd my-project
git init
git add .
git commit -m "first commit"

# ローカル完結の場合はここまで。以降は add → commit を繰り返す。

# Remote repo に接続する場合は GitHub で空リポジトリを作成してから実行
git remote add origin https://github.com/user/repo.git
git push -u origin main
```

### `git clone`:リモートリポジトリをローカルに取得する

GitHub 側でリポジトリを先に作り、手元に持ってくる場合。`init` + `fetch` + `checkout` を一括で行う。

```bash
git clone https://github.com/user/repo.git
cd repo
```

### `git remote add`:既存のローカルリポジトリをリモートに接続する

`git init` 済みのプロジェクトを後から GitHub に紐づける場合。

```bash
git remote add origin https://github.com/user/repo.git
git branch -M main
git push -u origin main
```

0 件のコメント:

コメントを投稿

人気の投稿