helen's blog

ずっとおもしろいことしてたいな。

ややこしいブランチ名をつけたらfetchできなくなった件

ちょっとvagrantに興味を持ってしまったので
どうせならGitに管理してもらおうと思ったら悲劇が起こりました

  1. study-infraブランチ作成
  2. ローカルにチェックアウト
  3. 自分のブランチということがわかったほうがいいだろうからブランチ名をhelen/study-infraにしよう
  4. helen/study-infraブランチ作成
  5. $ git fetch → エラー!
$ git fetch
error: Cannot lock ref 'refs/remotes/origin/helen/study-infra': 'refs/remotes/origin/helen' exists; cannot create 'refs/remotes/origin/helen/study-infra'
 ! [new branch]      helen/study-infra -> origin/helen/study-infra  (unable to update local ref)
error: some local refs could not be updated; try running
 'git remote prune origin' to remove any old, conflicting branches

変な参照が残ってるからgit remote prune originしてブランチのコンフリクト解消して!
だそうです

よくみるとhelen/study-infraとhelenというブランチが喧嘩している模様
helenブランチは必要ないので削除してみた

$ git fetch
error: Cannot lock ref 'refs/remotes/origin/helen/study-infra': 'refs/remotes/origin/helen' exists; cannot create 'refs/remotes/origin/helen/study-infra'
 ! [new branch]      helen/study-infra -> origin/helen/study-infra  (unable to update local ref)
error: some local refs could not be updated; try running
 'git remote prune origin' to remove any old, conflicting branches

だめでした
エラーメッセージどおり'git remote prune origin'してみると

$ git remote prune origin      
Pruning origin
 * [pruned] origin/detached-test
 * [pruned] origin/feature/create-backup-shell
 * [pruned] origin/helen
 * [pruned] origin/study-infra
$ git fetch
 * [new branch]      helen/study-infra -> origin/helen/study-infra

いけた

git remote prune とは
$ git remote --help

prune
   Deletes all stale remote-tracking branches under <name>. These stale branches have already been removed from the remote repository
   referenced by <name>, but are still locally available in "remotes/<name>".

リモートブランチは削除されてもローカルには追跡ブランチが残ってしまうのでその削除用らしい
たしかにログを見るとリモートで削除したブランチがpruneされている

つまりブランチを削除するなら
$ git remote prune originして追跡ブランチも消すとさらにすっきりするよ!
ということでした