# 作業用ディレクトリで開発する

開発用ブランチを作成したら
`git worktree`
で作業スペースを作成してください。

```bash
# プロジェクトルートに移動する
$ cd haniwers

# ブランチ名を確認する
(main) $ git branch
+ 44-feat-threshold-scan-parallel
+ docs-discuss-datamodel
  docs-for-developers
* main
  updated-tags

# 作業スペースを作成する
# 書式: git worktree add 相対パス ブランチ名
(main) $ git worktree add ../worktrees/hnw-docs-dev docs-for-developers

# 作業スペースに移動する
(main) $ cd ../worktrees/hnw-docs-dev
(docs-for-developers) $ poetry install
```

- `worktree`で作成するディレクトリはプロジェクトの外（`../worktrees/`）に作成することが推奨されています
- `worktree`を使うと、ブランチごとに開発環境を隔離して安全に作業できます

:::{tips}
**なぜ git worktree を使うのか？***

`git worktree`
で作成したブランチごとの作業スペースはお互いに隔離されています。
複数のブランチを同時に開発したり、
別のブランチを一時的に修正したりする場合に、
これまでの作業内容をそのままに、
ディレクトリを変更するだけで、ブランチを切り替えることができます。

同一のディレクトリでブランチ切り替えをする場合、
`git stash` や `git pop` が必要だったり、
いろいろ大変です。

:::

## 作業スペースで開発する

```bash
$ poetry install
$ poetry run pytest
$ poetry run ruff format
```

```bash
git add 編集したファイル名
git commit    # Conventional Commitにしたがったコミットメッセージ
git merge origin/main   # メインブランチの変更を取り込む
git push
```

## 作業スペースを削除する

```bash
# メインブランチに移動する
(docs-for-developers) $ cd ../../haniwers
(main) $

# メインブランチを最新状態にする
(main) $ git fetch --prune
(main) $ git pull

# worktreeを確認する
(main) $ git worktree list

# worktreeを削除する
(main) $ git worktree remove ../worktrees/hnw-docs-dev

# ブランチを削除する
(main) $ git br -d docs-for-dev
```
