# Git クイックリファレンス

*ブランチ、マージ、リベース、スタッシュ、リモート*

> Source: Pro Git (git-scm.com/book) · MIT

## セットアップ

### ユーザー設定

```
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor vim
git config --list     # show all settings
```

### 初期化 & クローン

```
git init              # new repo in current dir
git clone <url>       # copy remote repo
git clone <url> dir   # clone into dir/
```

## 基本操作

### ステータス & ステージング

```
git status            # working tree status
git add file.txt      # stage a file
git add .             # stage all changes
git add -p            # stage interactively
```

### コミット

```
git commit -m "msg"   # commit staged changes
git commit -am "msg"  # stage tracked + commit
git commit --amend    # edit last commit
```

### 差分確認

```
git diff              # unstaged changes
git diff --staged     # staged changes
git diff HEAD~1       # vs previous commit
git diff branchA branchB
```

## ブランチ

### ブランチ管理

```
git branch            # list local branches
git branch -a         # list all (incl. remote)
git branch feat       # create branch
git branch -d feat    # delete (safe)
git branch -D feat    # delete (force)
```

### ブランチ切り替え

```
git checkout feat     # switch to branch
git checkout -b feat  # create + switch
git switch feat       # switch (modern)
git switch -c feat    # create + switch (modern)
```

### マージ

```
git merge feat        # merge feat into current
git merge --no-ff feat # always create merge commit
git merge --abort     # cancel conflicted merge
```

## リモート

### リモート管理

```
git remote -v                # list remotes
git remote add origin <url>  # add remote
git remote remove origin     # remove remote
```

### フェッチ、プル、プッシュ

```
git fetch origin      # download updates
git pull              # fetch + merge
git pull --rebase     # fetch + rebase
git push origin main  # push to remote
git push -u origin feat # push + set upstream
```

## ログ

### 履歴の表示

```
git log               # full commit log
git log --oneline     # compact one-line format
git log --oneline -10 # last 10 commits
git log --graph --oneline --all # branch graph
git log -p            # show patches (diffs)
git log --stat        # show file change stats
```

### ログの絞り込み

```
git log --author="Alice"
git log --since="2024-01-01"
git log -- path/file  # commits touching file
git log -S "keyword"  # search content changes
```

## スタッシュ

```
git stash             # save working changes
git stash push -m "wip" # save with message
git stash list        # list all stashes
git stash pop         # apply + remove top stash
git stash apply       # apply but keep stash
git stash drop        # remove top stash
git stash clear       # remove all stashes
```

## 変更の取り消し

### アンステージ & 破棄

```
git restore file.txt           # discard working changes
git restore --staged file.txt  # unstage file
git checkout -- file.txt       # discard (legacy)
```

### リセット

| Command | Description |
|---------|-------------|
| `git reset --soft HEAD~1` | コミットを取り消し、ステージは保持 |
| `git reset --mixed HEAD~1` | コミットを取り消し、作業ツリーは保持（デフォルト） |
| `git reset --hard HEAD~1` | コミットを取り消し、全変更を破棄 |

### リバート

```
git revert <commit>   # new commit undoing changes
git revert HEAD       # undo last commit (safe)
```

## タグ

```
git tag v1.0                  # lightweight tag
git tag -a v1.0 -m "Release" # annotated tag
git tag                       # list tags
git push origin v1.0          # push single tag
git push origin --tags        # push all tags
git tag -d v1.0               # delete local tag
```

## .gitignore

### パターン例

```
# .gitignore
*.log           # all .log files
build/          # build directory
!important.log  # exception (do track)
/TODO           # only root TODO
doc/**/*.pdf    # pdfs in doc/ subtree
```

### よく使うパターン

| Command | Description |
|---------|-------------|
| `*.ext` | 特定の拡張子を持つ全ファイル |
| `dir/` | ディレクトリ全体 |
| `!pattern` | 除外対象を再包含 |
| `**/name` | 任意のサブディレクトリにマッチ |
| `?` | 1文字のワイルドカード |
