3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

--mirrorリポジトリをpost-commitで指定した場合の--git-dirに関して

Last updated at Posted at 2013-07-21

詳細はgitのbareリポジトリのバックアップをとるを参照してください。
というか基本的には--git-dir以外は全部一緒です。

参照先では、bareリポジトリをpost-commitを利用したバックアップ方法が紹介されています。
自分の場合はbareリポジトリではなく通常のローカルリポジトリをこの方法を利用してバックアップを取りたかったので設定してみました。
ちなみにgitのバージョンは以下の通りです。

home$ git --version
git version 1.8.3.2

では、実際にバックアップまでの手順です。

# 共用リポジトリからcloneします
git clone http://xxxx.zzzz.yyy local.git
# クローンしたローカルリポジトリから--mirrorでバックアップリポジトリを作ります。
git clone --mirror local.git backup.git

local.git.git/hocks/post-commitを作成します。

cd local.git/.git/hocks
touch post-commit
chmod +x post-commit

こんな感じにpost-commitを変更します。

# !/bin/sh

cd /tmp/work/backup.git && git --git-dir=. fetch --all -f

参照サイトと違うのは、--git-dirの部分で、参照先は``--git-dir=.gitとなっていましたがここでは--git-dir=.`となっている点です。
また、`git rebase -i`するとミラーリポジトリでエラーになってしまうので、`-f`を追記しています。

--git-dirの意味が(cdしたあとの)カレントディレクトリから相対的に.gitがどこかを示すと理解して、--mirrorの場合はそのディレクトリそのものと判断して--git-dir=.と指定したらうまく行きました。

これで、local.gitにコミットしたタイミングでこんな感じでログがでてローカルリポジトリにバックアップされます。

root$ git commit -m "commit message"
Fetching origin
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /tmp/work/local.git
   eb51dd3..74d93d4  master     -> master
[master 74d93d4] commit message
 1 file changed, 1 insertion(+)

余談

git --reset hard HEAD^

とかした後どうなるかなと思ったんですが、問題なくバックアップリポジトリに反映されるみたいです

git rebase -i XXXX

とかした場合はバックアップリポジトリには反映されません。rebaseの場合もバックアップに通知したいならpost-rbaseとかにも書けばいいと思います(未確認)。

20130724追記

git rebase -i でコミットを減らすとこんな感じでエラーになってしまいました。

fatal: bad object 8405f55743755504b6178bd338b2a7ea32f76e97
error: /Users/ymorika/sourcecodes/git/cloudmailer.git did not send all necessary objects

-fをつけてあげるとよいようです。

以上です。

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?