恥ずかしながらgitでリモートリポジトリ管理するのが初めてなので自分用のメモ。
やりたいこと
テーマファイル(~/wordpress/wp-content/themes/[テーマ名称]/配下の.phpや.cssなど)をリポジトリ管理します。
ローカルのgitでも良いんですが、webhookとか使えるのと自分の勉強もかねてgithubで管理します。
動作環境・前提
- Mac OS X High Sierra
- git version 2.20.1
- すでにローカルのwordpressにテーマファイルが存在している状態からスタート
- テーマファイルをgithubの1リポジトリで管理します
- github上でのリポジトリ作成は済んでいるものとします
テーマファイルをgithubのmasterブランチ管理するまで
作業手順
作業前の状態。
1 2 3 4 5 6 |
$ pwd ~/wordpress/wp-content/themes/[テーマ名称] $ ls functions.php page.php header.php images sidebar.php footer.php js style.css $ |
git init
でローカルリポジトリ化。
1 2 3 4 5 6 7 |
$ git init $ ls -la . .. .DS_Store .git 他 |
ローカルでmasterブランチ作成(git commitの実行)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$ git add -A $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) [省略] $ git commit -m "The first commit." Committer: User Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit [省略] $ $ git status On branch master $ git branch * master |
リモートリポジトリの設定。このコマンドを実行することでoriginという名前でurlのリモートリポジトリに対してアクセスできるようになる。urlの部分はgithubで確認要。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ cat -n .git/config 1 [core] 2 repositoryformatversion = 0 3 filemode = true 4 bare = false 5 logallrefupdates = true 6 ignorecase = true 7 precomposeunicode = true $ git remote add origin https://github.com/[githubのusername]/[githubのrepository_name].git $ cat -n .git/config 1 [core] 2 repositoryformatversion = 0 3 filemode = true 4 bare = false 5 logallrefupdates = true 6 ignorecase = true 7 precomposeunicode = true 8 [remote "origin"] 9 url = https://github.com/[githubのusername]/[githubのrepository_name].git 10 fetch = +refs/heads/*:refs/remotes/origin/* |
リモートリポジトリに対してpush。これでリモートリポジトリにmasterブランチが作成される。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ git push origin master Enumerating objects: 20, done. Counting objects: 100% (20/20), done. Delta compression using up to 4 threads Compressing objects: 100% (19/19), done. Writing objects: 100% (20/20), 64.12 KiB | 9.16 MiB/s, done. Total 20 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done. To https://github.com/[githubのusername]/[githubのrepository_name].git * [new branch] master -> master $ git branch -a * master remotes/origin/master |
すでにリモートリポジトリにmasterブランチがあるとgit pushがうまくいかない
ちなみにリモートリポジトリにmasterブランチがすでにある場合は、git push
しようとするとうまくいかない。
理由はリポジトリの履歴情報が一致しておらず、まったく別のリポジトリだと認識されているから。(実際まったく別のリポジトリなんだけど。。。)
なのでリモートリポジトリの履歴情報をローカルに引き込んであげれば良い。
1 2 3 4 |
$ git fetch origin master [省略] $ git merge --allow-unrelated-histories origin/master [省略] |
--allow-unrelated-histories
オプションは無関係なヒストリを持つ2つのブランチをマージするオプションらしい。いつかのgitバージョンからか追加された模様。
開発用のfeatureブランチ作成するまで
ローカルでfeatureブランチを作成する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ git branch * master remotes/origin/master $ git branch feature $ git branch -a * master feature remotes/origin/master $ git checkout feature $ git branch * feature master remotes/origin/master $ ls functions.php page.php header.php images sidebar.php footer.php js style.css |
リモートリポジトリに対してfeatureブランチをpushする。これでリモートリポジトリにもfeatureブランチができる。
1 2 3 4 5 6 7 8 |
$ git push origin feature [省略] * [new branch] feature -> feature $ git branch -a * feature master remotes/origin/feature remotes/origin/master |
ローカルブランチをリモートブランチと紐づける
git branch (ローカルブランチ名) -u origin/(リモートブランチ名)
でローカルブランチとリモートブランチを関連づけられる。そうしておくとローカルのfeatureからリモートのmasterにpushするといった誤った運用を防止できる。(と思われる。。。他にも意味があるんだと思うがよくわかってないので勉強。)
git branch -vv
で今の紐付けを確認できる。
1 2 3 4 5 6 7 8 9 10 |
$ git branch -vv * feature f981829 The first commit. master f981829 The first commit. $ git branch master -u origin/master Branch 'master' set up to track remote branch 'master' from 'origin'. $ git branch feature -u origin/feature Branch 'feature' set up to track remote branch 'feature' from 'origin'. $ git branch -vv * feature f981829 [origin/feature] The first commit. master f981829 [origin/master] The first commit. |