LoginSignup
198

More than 1 year has passed since last update.

gitのベアリポジトリとノンベアリポジトリ

Last updated at Posted at 2018-05-20

はじめに

リーナス・トーヴァルズはgitを10日間で作ったとか、神はいるんだねえ。

開発用Macでコーディング、その後サーバにpushして…という僕のような開発スタイルの場合、githubにpushするという仕組みに必要性を感じなくて、開発用クライアント(Mac)対本番用サーバ(linux)間でのgitの関係を理解して、やっとgitを使う必要性が見えてきたのでした。

サルわかもリモートリポジトリでBacklogあたりでわかんなくなってた。

gitのリポジトリとかの概念

branchとかmasterとかの以前に、リポジトリと作業ディレクトリの概念、bareとnon-bareリポジトリについて

bare[ベア]とは

bareとは裸の、とか剥き出しとかいう意味
ベアリポジトリは作業ディレクトリを持たない。更新されたとか情報のみを持つ。
ディレクトリ名は「hogehoge.git」のように".git"を付ける。

ここが一番わかりやすかった

※non-bareも.git付けてるのが気になるところですが(小声)

前提

クライアントとサーバ間はローカルLAN上にあり、sshログインできるようにしてあること
ディレクトリ名は以下のようにします。

  1. クライアントPC:開発用ディレクトリ(non-bareノンベアリポジトリ)→/home/clientusername/www_dev
  2. サーバ(IP:192.168.0.1)
    本番用ディレクトリ(non-bare)→/home/serverusername/www_product
    +リモートリポジトリ(bareベアリポジトリ)→/home/serverusername/www.git
    実際は開発用ディレクトリwww_devと本番用ディレクトリwww_productの名前は一緒なことが多いと思いますが、説明上わけがわからなくなるので。

まずはクライアントPCで開発(のつもり)

クライアント側[/home/clientusername]
$mkdir www_dev
$cd www_dev
$echo 'hello local!!' > index.html

gitの管理下(non-bareリポジトリ)にします

自分の理解ではnon-bareリポジトリ=作業用ディレクトリは同じで、
ディレクトリ名に.gitを付けないことでベアリポジトリと区別されます。
www_devにいる状態でgit initします。

クライアント側[/home/clientusername/www_dev]
$git init
$git add .
$git commit -m 'first commit'

次にリモートリポジトリの準備

サーバ側にリモートリポジトリを作成
init時 --sharedオプションを付けるのはグループで共有するため(パーミッションが違う)
個人ユーザーのみの場合は特に付けなくても良い。

サーバ側[/home/serverusername]
$mkdir www.git
$cd www.git
$git init --bare --shared

PC側からリモートリポジトリにpushする

gitの解説サイトでoriginっていうのを盲目的に使っているが、要はエイリアス名なのでなんでもいい。
僕もなんでoriginなんだろう。弁当かよ…って思ってましたが、これを読んで納得。

クライアント側[/home/clientusername/www_dev]
$git remote add origin ssh://serverusername@192.168.0.1:/home/serverusername/www.git

↓originじゃなくてもhottomottoでもいいのです。僕はほっかほっか亭派なのでhokkahokkaで登録

クライアント側[/home/clientusername/www_dev]
$git remote add hokkahokka ssh://serverusername@192.168.0.1/home/serverusername/www.git

確認してみます。

クライアント側[/home/clientusername/www_dev]
$git remote -v

↓ sshのポートを22番から変えている人は:port_noが必須です。

クライアント側[/home/clientusername/www_dev]
hokkahokka      ssh://serverusername@192.168.0.1:port_no/home/serverusername/www.git (fetch)
hokkahokka      ssh://serverusername@192.168.0.1:port_no/home/serverusername/www.git (push)

これでpushの準備は完了。
一旦ここでサーバ側を見てみることにします。

サーバ側[/home/serverusername/www.git]
$git log
fatal: your current branch 'master' does not have any commits yet

マスターブランチにはまだコミットが無いよ!と叱られます。

いよいよpushです。

クライアント側[/home/clientusername/www_dev]
$git push hokkahokka master

ずらずらとファイルが転送されたご様子。
さてサーバ側を見てみましょう

サーバ側[/home/serverusername/www.git]
$git log
commit d14a1c0bc0b3b46814c99cb655018da8941b1a88
Author: hogehoge <hogehoge@gmail.com>
Date:   Tue Jan 16 16:05:38 2018 +0900

    first commit

さっきfetalが出ていたのに、ちゃんとfirst commitが反映されています。
重要なのはリモートリポジトリはbareであるという点です。
lsでディレクトリを見てみてください。

サーバ側[/home/serverusername/www.git]
HEAD  branches  config  description  hooks  info  objects  refs

クライアント側で作成したindex.htmlはここには存在しません。

サーバ側の本番用作業ディレクトリをcloneする

これはサーバ側でcloneするだけなのでリモートリポジトリディレクトリの上にディレクトリを移動してから

サーバ側[/home/serverusername]
$git clone www.git

とするとwwwディレクトリが作成されます。

ディレクトリ名をbareリポジトリと違う名称にしたい場合は最後にディレクトリ名を付けます。

サーバ側[/home/serverusername]
$git clone www.git www_product

www_productディレクトリができました。
index.htmlがあるだけ。中身は"hello local!"です。

[/home/serverusername/www_product]
$ls
index.html
$cat index.html
hello local!

git logを見てみましょう

サーバ側[/home/serverusername/www_product]
$git log
commit d14a1c0bc0b3b46814c99cb655018da8941b1a88
Author: hogehoge <hogehoge@gmail.com>
Date:   Tue Jan 16 16:05:38 2018 +0900

    first commit

ちなみにこのディレクトリはbareリポジトリをcloneしただけなので
リモートリポジトリはoriginです。hokkahokkaはクライアント側のエイリアスだったことがわかります。

サーバ側[/home/serverusername/www_product]
$git remote -v
origin	/home/serverusername/www.git (fetch)
origin	/home/serverusername/www.git (push)

クライアント側で修正

ファイルの中身をhello local!から「hello world!!!!!!」に変更します。
その後コミットし、pushします。

クライアント側[/home/clientusername/www_dev]
$echo 'hello world!!!!!!' > index.html
$git add .
$git commit -m 'second commit'
$git push hokkahokka master

サーバ側で確認するとsecond commitが反映されています。

サーバ側[/home/serverusername/www.git]
$git log
commit 59ad5e9b565863600af9c3f5f2b4941ffeaed188
Author: hogehoge <hogehoge@gmail.com>
Date:   Tue Jan 16 23:18:18 2018 +0900

    second commit

commit d14a1c0bc0b3b46814c99cb655018da8941b1a88
Author: hogehoge <hogehoge@gmail.com>
Date:   Tue Jan 16 16:05:38 2018 +0900

    first commit

どこが変わっているのか確認したい場合はこちらを参照

本番用ディレクトリでpullする

サーバ側[/home/serverusername/www_product]
$git pull origin master

ちゃんと本番用にもsecond commitが反映されています。

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
198