LoginSignup
10
1

More than 3 years have passed since last update.

git worktree でブランチ名にスラッシュが入っている時の対処法

Last updated at Posted at 2020-04-11

ブランチごとにディレクトリを分けることができるgit worktree
名前にスラッシュが含まれるブランチを新規作成する際にハマったので、備忘録を兼ねてメモ。

まず、シンプルなブランチ新規作成方法

> git worktree add {ブランチ名}

masterブランチからdevelopブランチを作成

master配下で下記の通り実行。

> git branch
* master

> git worktree add develop
Preparing worktree (new branch 'develop')
HEAD is now at 7e47e15 Initial commit

> git worktree list
~/         7e47e15 [master]
~/develop  7e47e15 [develop] # masterと同じハッシュ

> ls
... develop # developブランチのディレクトリが新規作成

ブランチ名にスラッシュが含まれる場合の新規作成方法

本題。

> git worktree add {作成するディレクトリのパス} -b {ブランチ名}

developブランチからfeature/#1ブランチを作成

develop配下で下記の通り実行。

> git worktree add ./../feature/#1 -b feature/#1
Preparing worktree (new branch 'feature/#1')
HEAD is now at 9150323 add: a.py

> git branch      
* develop
+ feature/#1
+ master

> git worktree list
~             7e47e15 [master]
~/develop     9150323 [develop]
~/feature/#1  9150323 [feature/#1] # developと同じハッシュ

ちなみにシンプルな方法で行うと、末尾から最も近いスラッシュ以下がブランチ名だとみなされてしまう。

> git worktree add feature/#2
Preparing worktree (new branch '#2')
HEAD is now at 9150323 add: a.py

> git branch
+ #2 # feature/#2にしたいのに!!
+ develop
* feature/#1
+ master

10
1
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
10
1