0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

git worktree の使い方メモ

0
Posted at

毎回毎回、忘れてしまうので、メモ書きとして作成。

git worktree とは

1つのリポジトリから複数の作業ディレクトリを同時に展開できる git の機能です。
ブランチを切り替えずに、別のブランチの作業を並行して行いたい場合に便利です。

基本的な考え方

通常、git リポジトリには、1つの作業ディレクトリしかありません。
別のブランチで作業したい場合は、ブランチを切り替える必要があります。
しかし、その度に変更内容をstashしたり、コミットする必要があります。

そこで、git worktree を使うと、別のディレクトリに別ブランチをチェックアウトした状態を作れるので、この手間がなくなります。

よく使うコマンド

ここからは、よく使うコマンドの紹介と解説を行います。

Add: 新しいワークツリーを追加する

git worktree add ../feature-branch feature-branch-name

これは、現在いるディレクトリ階層から1つ前の階層に feature-branch というディレクトリを作成し、feature-branch-name ブランチがチェックアウトされている状態になります。新しいブランチを同時に作成したい場合は -b を追加します。

git worktree add -b new-feature-branch ../new-feature main

これは、main ブランチから new-feature-branch ブランチを作り、../new-feature ディレクトリに展開します。

list: ワークツリーを一覧で確認する

git worktree list

現在、存在するワークツリーと、それぞれのブランチを表示します。
どのようなワークツリーを作成したか確認したい場合、このコマンドを使います。

Remove: 不要になったワークツリーを削除する

git worktree remove ../feature-branch

ディレクトリごと削除します。もし、ディレクトリを手動で削除した場合は、

git worktree prune

で管理情報をクリーンアップできます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?