hachinoBlog

hachinobuのエンジニアライフ

自宅のCentOSにGitサーバを構築してみた

CentOS6.4にGitサーバを構築したのでメモ

[サーバ側]

# yum install git git-deamon git-all xinetd
# chkconfig xinetd on
# /etc/init.d/xinetd start
xinetd を起動中:                                           [  OK  ]

必要なものをインストールして起動設定が終わったらgit-daemonファイルを/etc/xinetd.dに作成

# touch /etc/xinetd.d/git-daemon
# vi /etc/xinetd.d/git-daemon

内容は

# default: off
# description: The git dæmon allows git repositories to be exported using \
#       the git:// protocol.

service git
{
        disable         = no      # <- 変更
        socket_type     = stream
        wait            = no
        user            = nobody
        server          = /usr/libexec/git-core/git-daemon
        server_args     = --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose
        log_on_failure  += USERID
}

設定を反映するために再起動

# /etc/rc.d/init.d/xinetd restart

リポジトリ作成

# cd /var/lib/git/
# mkdir public_git
# mkdir public_git/test.git
# cd public_git/test.git
# git --bare init --shared
Initialized empty shared Git repository in /var/lib/git/public_git/test.git/

gitグループを作成してユーザをそのグループに追加

# groupadd git
# usermod -G wheel,git hachinobu
# chown -R root:git /var/lib/git/


[クライアント側]
環境はgitコマンドが使用できる状態であることを前提

$ cd ~/src
$ mkdir test
$ cd test
$ echo "Git Test." > test.txt

ローカルリポジトリの作成

$ git init
Initialized empty Git repository in /home/hachinobu/src/test/.git/

ローカルにコミット

$ git add test.txt
$ git commit -m "First Commit"

リモートリポジトリの登録

git remote add test ssh://hachinobu@server-name.com:56722/var/lib/git/public_git/test.git

リモートリポジトリに push

$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 229 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://hachinobu@server-name.com:56722/var/lib/git/public_git/test.git
 * [new branch]      master -> master

ssh 用の鍵ペアを登録していればパスワードは求められない

リモートリポジトリから Pull

$ git pull test master
From ssh://hachinobu@server-name.com:56722/var/lib/git/public_git/test.git
 * branch            master     -> FETCH_HEAD
Already up-to-date.

リモートリポジトリを Clone

cd ..
$ rm -rf ~/src/test
$ git clone ssh://hachinobu@server-name.com:56722/var/lib/git/public_git/test.git
Cloning into 'test'...
remote: Counting objects: 3, done.
Receiving objects: 100% (3/3), 228 bytes, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
$ ls -la test
合計 16
drwxr-xr-x  3 hachinobu hachinobu 4096 731 22:12 .
drwxr-xr-x 13 hachinobu hachinobu 4096 731 22:12 ..
drwxr-xr-x  8 hachinobu hachinobu 4096 731 22:12 .git
-rw-r--r--  1 hachinobu hachinobu   10 731 22:12 test.txt


まさにここに書いてある通りでした
http://www.mk-mode.com/octopress/2012/12/07/centos-construct-git-server/