UbuntuにGitサーバ構築。Macでローカルリポジトリを運用してみる。

4.1 Git サーバー - プロトコル | Git を参考に。

  • Gitサーバを立てるホスト名は argyle.mnb 。アクセスのプロトコルSSH とする。
  • リモートリポジトリは argyle.mnb:/var/repos/git に作成。これは git アカウントをオーナとする。
  • ローカルリポジトリMacBook Pro に作成。これは naoyes アカウントで、とする。

argyle.mnb には Ubuntu が載っています。経緯はこちらあたりに。

Gitサーバの構築

1. argyle.mnb に git アカウントを作成

以後当アカウントを git@argyle.mnb と表現します。
rcs というグループがありますがこちらは Subversionの設置 の時に設定したものですね。

# git アカウントを作成
argyle.mnb$ sudo useradd -m git -g rcs
argyle.mnb$ id -a git
uid=1003(git) gid=1001(rcs) groups=1001(rcs)

# Git のリポジトリ用ディレクトリを作成
argyle.mnb$ cd /var/repos/
argyle.mnb$ sudo mkdir git
argyle.mnb$ sudo chown git:rcs git
2. MacBook Pro の naoyes アカウントの公開鍵を作成

以後当アカウントを naoyes@mac と表現します。

# 公開鍵を作成
naoyes-mac:~ naoyes$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/naoyes/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): # [Enter]のみ
Enter same passphrase again: # [Enter]のみ
Your identification has been saved in /Users/naoyes/.ssh/id_rsa.
Your public key has been saved in /Users/naoyes/.ssh/id_rsa.pub.
The key fingerprint is:
be:bf:e1:7e:cd:11:5d:ce:f3:af:49:15:4c:1f:4a:6a naoyes@naoyes-mac.local
The key's randomart image is:   # 'blogのシンタックスカラー用
+--[ RSA 2048]----+
|             . o |
|            o + +|
|           E . *o|
|          .   ..=|
|        S      .+|
|       .      ...|
|        . .  o...|
|         o ...o..|
|        .o=o  o. |
+-----------------+
naoyes-mac:~ naoyes$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMAdHPBFT9CCR8ti9/JOQWpaxQZTl9+RQIHYIjuFXdnQwBbjxbD32PaOHJFDNExgb/F3NH2vE5IfDU12RHZ8Ph2bV+atXj9nNEYOgjIok6Z4sc6olsLh+PvckeZrl8BhrClpyYhAubBNDLxVJwabnJ1C1QV2SZM2khfy8VdQdMf4lmEKJfDdPIJh9aPf5lD+aFNNpCjEXv6asCUb5k8mu+PKjGIHwTZ4icXvZsVlomxlITi7Ra3V43OAuIGi9yV14XvLQWvRC8nzPQG1Vu6N9cq+EDu1sSdRdj3eZNAlKbBbtiOvM8F28m1kcz27HcdoFasAh09R5JmhmOro2r+n4d naoyes@naoyes-mac.local

# argyle.mnb に公開鍵を複製
naoyes-mac:~ naoyes$ scp .ssh/id_rsa.pub naoyes@argyle.mnb:/tmp/naoyes_id_rsa.pub
naoyes@argyle.mnb's password: 
id_rsa.pub                                                                                                                 100%  406     0.4KB/s   00:00    
3. git@argyle.mnb に naoyes@mac の公開鍵を登録
# 公開鍵を登録
argyle.mnb$ sudo su git
argyle.mnb$ whoami
git
argyle.mnb$ pwd
/home/git
argyle.mnb$ cat /tmp/naoyes_id_rsa.pub >> .ssh/authorized_keys 

# テンポラリファイルを消しておく
argyle.mnb$ exit
argyle.mnb$ whoami
naoyes
argyle.mnb$ rm -r /tmp/naoyes_id_rsa.pub

使ってみる

1. リモートリポジトリを作成
argyle.mnb$ sudo su git
argyle.mnb$ cd /var/repos/git
# ベアリポジトリとして作成
argyle.mnb$ git init --bare demo.git
Initialized empty Git repository in /var/repos/git/demo.git/
argyle.mnb$ ls -la | grep demo
drwxr-xr-x 7 git rcs 4096 2012-05-20 10:17 demo.git
2. naoyes@mac における git のパスを通す

MacにはXcodeがすでにインストールされています。
このXcodeと一緒にバンドルされている git があるのですがデフォルトではそこにパスが通っていないよう。
わざわざ別途インストールすることもないかなということでパスを通しておきます。

naoyes-mac:~ naoyes$ which git
naoyes-mac:~ naoyes$ echo 'export PATH=/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core:$PATH' >> ~/.bash_profile
naoyes-mac:~ naoyes$ source ~/.bash_profile 
naoyes-mac:~ naoyes$ which git
/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/git
3. ローカルリポジトリを作成
# クローンする
mac$ pwd
/home/naoyes/tmp/git
mac$ git clone git@argyle.mnb:/var/repos/git/demo.git # URI は git@{gitserver}:/path/to/git とする
Cloning into demo...
warning: You appear to have cloned an empty repository.
mac$ la | grep demo
drwxr-xr-x 3 naoyes naoyes 4096 2012-05-20 10:17 demo
4. ローカルリポジトリからリモートリポジトリへのプッシュ
# ローカルリポジトリでの作業
mac$ pwd
/home/naoyes/tmp/git/demo
mac$ touch hoge
mac$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       hoge
nothing added to commit but untracked files present (use "git add" to track)
mac$ git add hoge
mac$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   hoge
#
mac$ git commit -m 'initial commit'
[master (root-commit) 847b70b] initial commit
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hoge

# 設定されてるリモートを確認
mac$ git remote -v
origin  git@argyle.mnb:/var/repos/git/demo.git (fetch)
origin  git@argyle.mnb:/var/repos/git/demo.git (push)

# プッシュ
mac$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@argyle.mnb:/var/repos/git/demo.git
 * [new branch]      master -> master
5. ちゃんとプッシュされているかをリモートリポジトリで確認
argyle.mnb$ sudo su git
argyle.mnb$ pwd
/var/repos/git/demo.git
argyle.mnb$ git log
commit 847b70b11b42a11ed39ccbf8504f8286a5118111
Author: naoyes <naoyes@gmail.com>
Date:   Sun May 20 10:19:29 2012 +0900

    initial commit

OKです!

おまけ

リポジトリパスの変更

git@argyle.mnb:/var/repos/git/demo.git と、argyle.mnb における絶対パスが見えちゃうのがなんとなく嫌なので git@argyle.mnb においてホームディレクトリからシンボリックリンクを貼ってやりました。英語で"リポジトリー"は"repos"よりも"repo"と略すほうがどうも一般的らしいのでそのようにしてあります。

argyle.mnb$ sudo su git
argyle.mnb$ whoami
git
argyle.mnb$ cd ~
argyle.mnb$ ln -s /var/repos/git repo

ローカルリポジトリでリモートの設定を変えてやります。
ま、普段は名前"origin"でアクセスなのでそんなに気にすること無いんでしょうけどね。

mac$ pwd
/home/naoyes/tmp/git/demo
mac$ git remote -v
origin  git@argyle.mnb:/var/repos/git/demo.git (fetch)
origin  git@argyle.mnb:/var/repos/git/demo.git (push)
mac$ git remote rm origin
mac$ git remote -v
mac$ git remote add origin git@argyle.mnb:repo/demo.git
mac$ git remote -v
origin  git@argyle.mnb:repo/demo.git (fetch)
origin  git@argyle.mnb:repo/demo.git (push)