gitでbareとnon-bareを切り替える

用語集

  • non-bare repository: 皆様のお手元にある普通のリポジトリがそれです。
  • bare repository: 普通はgitサーバーにある。手元でも取り扱い可能。ただしworking directoryがないので、work treeを必要とする様々な操作ができない。

bare repositoryは hoge.git/ のように .git という拡張子のついたディレクトリであらわすのが慣例であるため、それに従う。

cloneでbareからnon-bare, non-bareからbareを作成する

cloneするときにbareにするかnon-bareにするか指定できる。

# bareにする
git clone --bare /path/to/my-nonbare-repository /path/to/my-bare-repository.git

# non-bareにする
git clone /path/to/my-bare-repository.git /path/to/my-nonbare-repository

その場でnon-bareをbareにする

my-progress というnon-bare repositoryがあったとする。

cd /path/to/my-progress

# core.bare を入れる。この時点でwork treeは無視される。
git config core.bare true

# .git を取り出して my-progress.git にする。
mv .git ../my-progress.git

# 残されたwork treeはいらないので削除する。
cd ../my-progress.git
rm -r ../my-progress

その場でbareをnon-bareにする

my-progress.git というbare repositoryがあったとする。

cd /path/to/my-progress.git

# work tree 予定地を用意する
mkdir ../my-progress
cd ../my-progress

# bare repository を .git という名前にしてwork tree内に移動する
mv ../my-progress.git .git

# core.bare を外す。これでwork treeの存在が認識されるようになる。
git config core.bare false

# indexの中身をwork treeに展開する。
git checkout-index -a

まとめ

bare repositoryとnon-bare repositoryの違いは3点

  • core.bare configが true に設定されており、これによってwork treeがないものとして扱われる。
  • gitの管理ディレクトリが .git ではなく、 *.git という名前になっている。これにより、その外側のディレクトリはリポジトリの一部として扱われない。管理ディレクトリのみがリポジトリ本体として扱われる。