【GIT】エックスサーバー上にあるwordpressを丸ごとGIT管理してローカルで開発出来るようにする

⭕️やりたいこと

エックスサーバーでwordpressを置いてサイトを運営しているが、今までftpでファイルを上げたりしていたが丸ごとgit管理して、ローカルで修正してpushしてアップしたり、wordpress側でアップロードした画像等をローカルにも置いておけるようにしたい。

⭕️環境

ローカルPCはMAC

前提として、エックスサーバーに既にSSH接続出来ている事

ローカルにソースツリーもインストール済み

エックスサーバーのアカウントは:「xs123456」ということにする。

↑これがエックスサーバー上のwordpress

ここを丸ごとgit管理する

ローカルには

「wordpress」という空のディレクトリを作っておく

ここにクローンしてgit管理したい

 

エックスサーバー側の作業

まずエックスサーバーにsshでログインして、wordpressのディレクトリまでおります。

$ cd public_html 
$ ll
合計 220
-rw-r--r-- 1 xs123456 members 6633 5月 23 2020 default_page.png
-rw-r--r-- 1 xs123456 members 405 4月 1 2020 index.php
-rw-r--r-- 1 xs123456 members 19915 4月 15 2021 license.txt
-rw-r--r-- 1 xs123456 members 7278 3月 12 2022 readme.html
-rw-r--r-- 1 xs123456 members 6912 4月 1 2020 wp-activate.php
drwxr-xr-x 9 xs123456 members 4096 5月 24 2020 wp-admin
-rw-r--r-- 1 xs123456 members 351 4月 1 2020 wp-blog-header.php
-rw-r--r-- 1 xs123456 members 2332 6月 13 2020 wp-comments-post.php
-rw-r--r-- 1 xs123456 members 3931 4月 1 2020 wp-config-sample.php
-rw------- 1 xs123456 members 4235 5月 24 2020 wp-config.php
drwxr-xr-x 7 xs123456 members 127 7月 25 2022 wp-content
-rw-r--r-- 1 xs123456 members 3940 4月 1 2020 wp-cron.php
drwxr-xr-x 21 xs123456 members 8192 4月 1 2020 wp-includes
-rw-r--r-- 1 xs123456 members 2496 4月 1 2020 wp-links-opml.php
-rw-r--r-- 1 xs123456 members 3300 4月 1 2020 wp-load.php
-rw-r--r-- 1 xs123456 members 47874 4月 1 2020 wp-login.php
-rw-r--r-- 1 xs123456 members 8509 5月 24 2020 wp-mail.php
-rw-r--r-- 1 xs123456 members 19396 5月 24 2020 wp-settings.php
-rw-r--r-- 1 xs123456 members 31111 4月 1 2020 wp-signup.php
-rw-r--r-- 1 xs123456 members 4755 4月 1 2020 wp-trackback.php
-rw-r--r-- 1 xs123456 members 3133 4月 1 2020 xmlrpc.php

↑このディレクトリを丸ごとgit管理します。

⚫︎まずはgit化

$ git init
Initialized empty Git repository in /home/xs123456/public_html/wordpress.git/

⚫︎ベアリポジトリ作成

$ git clone --bare --shared /home/xs123456/wordpress/public_html/ wp.git
Cloning into bare repository 'wp.git'...
warning: You appear to have cloned an empty repository.
done.

この段階で

「.git」「wp.git」というディレクトリができています。

⚫︎無視リスト作成

vi .gitignore
wp.git

↑.gitignoreを作成して、wp.gitを無視してね!

と記載します。

これをやらないと今後増え続けるgitファイルを全部管理する事になっちゃいます。

⚫︎次に今作成したベアリポジトリから自動でpullする設定を入れます。

cd wp.git/hooks/

vi post-receive
⇩ファイル作成の中身
#!/bin/sh
#
cd /home/xs123456/wordpress/
git --git-dir=.git pull /home/xs123456/wordpress/wp.git/

:wqで保存

⇩権限付与、これやらないご動けない
chmod +x post-receive

⚫︎リモートレポジトリの登録

git remote add origin /home/xs377936/wordpress/public_html/wp.git

⚫︎名前とメールアドレスの登録

$ git config user.email test@test.com
$ git config user.name test

これをしとかないと怒られます。

これはgit側に操作してるの誰やねん、という事を教えています。

⚫︎この段階の状況を確認します。

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .htaccess
# .user.ini
# default_page.png
# index.php
# license.txt
# readme.html
# wp-activate.php
# wp-admin/
# wp-blog-header.php
# wp-comments-post.php
# wp-config-sample.php
# wp-config.php
# wp-content/
# wp-cron.php
# wp-includes/
# wp-links-opml.php
# wp-load.php
# wp-login.php
# wp-mail.php
# wp-settings.php
# wp-signup.php
# wp-trackback.php
# wp.git/
# xmlrpc.php
nothing added to commit but untracked files present (use "git add" to track)

未登録のファイルやディレクトリがいっぱいありますよ!!

と教えてくれます。

登録しましょう。

⚫︎未登録の色々をgitに追加します。

$ git add .

$ git commit -m 'first commit'

ずらずらー〜〜〜〜

ずらずら〜、の位置にwordpress内の全てのファイルが表示されると思います。

このファイルをgitに追加しますね!

と教えてくれています。

それではpushしましょう!

その前に状況確認

$ git status
# On branch master
nothing to commit, working directory clean

未登録や未編集のファイルはないぜ!

という状況

pushします

$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
To /home/xs123456/wordpress/public_html/wp.git
* [new branch] master -> master

うまくいっています。

最後にベアリポジトリまでのパスを確認します。

$ pwd
/home/xs123456/wordpress/public_html/wp.git

⚫︎ローカルのsoursetree側の作業

それではデスクトップに戻って作業します。

sourcetreeでクローンします。

「新規」→「URLからクローン」をクリック

さきほど確認したベアリポジトリまでのパスをsorcetreeに登録します。

ソースURLは⇩

ssh://test/home/xs123456/wordpress/public_html/wp.git

「test」の箇所ですが、自分で設定したsshの名前をいれます。

左したに「これはGitリポジトリです」と出れば成功

このままクローンをクリック

wordpressの全ファイルがクローンでローカルに落とされます。

↑こうなれば成功

ローカルのディレクトリを確認してみます。

↑ちゃんと全部落ちています!!

意外に簡単に出来ました。

これで開発が楽になるぞー!!

コメント