LoginSignup
6
6

More than 5 years have passed since last update.

git push でリモートのサーバにリポジトリを作成する

Last updated at Posted at 2014-04-18

流行っているようなので書いてみました。

authorized_keys の設定

.ssh/authorized_keys に command を書いておきます

command="/home/hexa/git.sh" ssh-rsa AAAAB3...

スクリプトは下記の内容です

git.sh
#!/bin/bash

set -- $SSH_ORIGINAL_COMMAND
case $1 in
  git-receive-pack ) REPOSITORY_PATH=$( echo $2 | tr -d "'" )
                     [ ! -e $REPOSITORY_PATH ] && /usr/bin/git --bare init $REPOSITORY_PATH > /dev/null 2>&1
                     /usr/bin/git-shell -c "$SSH_ORIGINAL_COMMAND" ;;
   git-upload-pack ) /usr/bin/git-shell -c "$SSH_ORIGINAL_COMMAND" ;;
                "" ) bash ;;
                 * ) $SSH_ORIGINAL_COMMAND ;;
esac

確認

まだリポジトリが無いリモートのサーバへ、ローカルから git push します

$ ssh hexa@192.168.1.8 'ls bare.git'
Enter passphrase for key '/Users/Hexa/.ssh/id_rsa':
ls: cannot access bare.git: No such file or directory

$ git push ssh://hexa@192.168.1.8:22/home/hexa/bare.git master
Enter passphrase for key '/Users/Hexa/.ssh/id_rsa':
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (9/9), 604 bytes, done.
Total 9 (delta 0), reused 0 (delta 0)
To ssh://hexa@192.168.1.8:22/home/hexa/bare.git
 * [new branch]      master -> master

$ ssh hexa@192.168.1.8 'ls bare.git'
Enter passphrase for key '/Users/Hexa/.ssh/id_rsa':
branches
config
description
HEAD
hooks
info
objects
refs

確認のため git clone もしてみます。

$ git clone ssh://hexa@192.168.1.8:22/home/hexa/bare.git
Cloning into 'bare'...
Enter passphrase for key '/Users/Hexa/.ssh/id_rsa':
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9/9), done.
6
6
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
6
6