LoginSignup
78
38

More than 3 years have passed since last update.

Gitでブランチを作ろうとしたら「fatal: cannot lock ref ...」と怒られた

Last updated at Posted at 2019-05-31

経緯

他の人が作った project/feature_name みたいなブランチを派生元として project/feature_name/add_hoge みたいな作業用ブランチを作ろうとしたら怒られた。

$ git branch
master
* project/feature_name

$ git checkout -b project/feature_name/add_hoge
fatal: cannot lock ref 'project/feature_name/add_hoge':
'project/feature_name' exists; cannot create 'project/feature_name/add_hoge'

原因

Git では foo/bar みたいなブランチを作ろうとすると .git ディレクトリ配下に foo ディレクトリを作ろうとする。
このとき既に foo というブランチが存在していると、当然もう foo ディレクトリが作成済みなので上記のようなエラーになるらしい。

$ git co -b hoge
Switched to a new branch 'hoge'
$ git co -b hoge/fuga
fatal: cannot lock ref 'refs/heads/hoge/fuga': 'refs/heads/hoge' exists; cannot create 'refs/heads/hoge/fuga'

参考: gitでfooとfoo/barというブランチは同時には作成できない

解決

1. 派生元のブランチ名を工夫する

今回のケースみたいにトップブランチを作って、そこから作業用ブランチを切って…みたいにやる場合、トップブランチ名を下記のようにすれば衝突しない。

NG: project/feature_name
OK: project/feature_name/top

上記のようにすると、 project/feature_name/add_hoge のように作業用ブランチの命名ができる。

2. 作業用ブランチの名前に / を使わない

個人的にはしっかり / で区切りたいけど、そんなこだわりはない場合は単に使わなければ良い。

例) project/feature_name_add_hoge
例) project/feature_name--add_hoge
例) project/feature_name__add_hoge

78
38
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
78
38