【備忘録】gitをxserverに設定しSourceTreeで本番環境にuploadする手順

IT技術系

私はxserverをサーバーに利用していますが、開発にはgitが不可欠ですよね。
今回は、xserverにgitを構築し、SourceTreeを使ってuploadする手順を説明していきます。皆様の開発の一助になりましたら、幸いです。

xserverの設定をします。

teratermでssh接続をします。 ※下記はbashコマンドです。

①ノンベアリポジトリにしたいディレクトリに移動します。
※今回はwordpressのthemesフォルダです。

$ cd ~/example.com/public_html/wp-content/themes/

②ノンベアリポジトリを作成します。

$ git init

③ユーザー情報を設定します。

$ git config user.email "maedamomotann@maedakikaku.jp"
$ git config user.name "yutaka maeda"

④最初のコミットをします。

$ git add .
$ git commit -m "first commit"

⑤ベアリポジトリにしたいディレクトリに移動します。
※public_htmlより上の階層にしておきましょう。

$ cd ~/example.com/

⑥ベアリポジトリを作成します。

$ git clone --bare --shared ~/example.com/public_html/wp-content/themes/ example.git

⑦プルリクエストの設定をします。

$ cd example.git/hooks
$ vi post-receive

⑧ファイルの中身は下記のように設定します。

#!/bin/bash
cd ../public_html/wp-content/themes/
git --git-dir=.git pull ../../../example.git master

⑨ファイルの属性を755にしておきましょう。

$ chmod 755 ./post-receive

SourceTreeの設定をします。

puttyにprivateKeyを登録

「ツール」⇒「キーの作成/インポート」
xserverで作成したprivateKeyをloadして、「Save private key」でputty用のkeyファイルを作成します。

常駐のputtyからprivateKeyを「addKey」します。

SourceTreeでリモートのcloneを作成します。

「ファイル」⇒「新規/クローンの作成」⇒「clone」を選択

【元のパス/URL】
ssh://サーバーID@サーバーID.xsrv.jp:10022/home/サーバーID/ドメイン/example.git
※サーバーID、ドメインはxserverのものです。

⇒20200128追記(ロリポップの場合のアドレス)
ssh://lolipop.jp-xxxxxx@ssh???.lolipop.jp:2222/home/users/?/lolipop.jp-xxxxxx/web/example.git

【保存先のパス】
C:\pleiades\xampp\htdocs\example

【名前】
example

「クローン」ボタンを押すと、cloneが作成されます。

後は、SourceTreeでadd⇒commit⇒pushとすれば、リモートのベアリポジトリが更新され、ノンベアがプルして、本番環境に更新されます。

お疲れ様でした(^^♪