始まりはgithubでプルリクが出せない!?
というものでしたが色々絡んでいて少し大変だった時の記録です。
前提、状況
今回新規でwordpressサイト立ち上げ時にローカルで開発して
featureブランチでプッシュ、そのあとmasterに
最初のプルリクエスト時にタイトルのメッセージが表示された。
そしてCompare & pull requestのボタンも見当たらないという状況。
エラーメッセージ
The diff you're trying to view is too large. We only load the first 3000 changed files.
原因
wp-contentというディレクトリごと管理する方式をとっており
pluginディレクトリ、さらにはデフォルトのテーマも含んでいた。
タイトルの通り3000ファイル以上に変更が発生してしまった計算になった模様。
対応
githubのヘルプによると色々と制限があるようです。
ならばということでまずはいらないテーマを削除してプッシュしてみたら
Compare & pull requestのボタンは出たものの新たなエラーメッセージが
There isn’t anything to compare.
master and feature are entirely different commit histories.
とりあえずローカルのmasterブランチを最新にして
ローカルmaster → リモートmasterのプッシュを試みる
$ git status
nothing to commit, working tree clean
$ git checkout master
$ git status
nothing to commit, working tree clean
$ git merge feature
$ git push origin master
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:example/example.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
今回githubのリモートリポジトリを手動で作ったことが
ここでエラーの原因になっている模様。
要はmasterブランチがローカルとリモートでうまくリンクできていない状態。
(ちなみにgit remote -v , ssh -T git@github.com などのコマンドは正常に通っており、featureはローカルからプッシュが通る状態なのでなぜ?という印象でした。)
一旦プルしてという指示があったのでやってみる。
ローカルの既存ファイルが消えないかなという不安もあったので
一応macのfinderから手動でディレクトリごとバックアップコピーを取っておく。
$ git branch
feature
* master
$ git pull origin master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:example/example
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
fatal: refusing to merge unrelated histories
成功したかと思いきやpushと同じ関連のない履歴ということで拒否されている。
どうやらGit 2.9から mergeコマンドとpullコマンドで
関連のないヒストリーを持つブランチをマージするには
オプション--allow-unrelated-historiesをつける必要があるとのこと。
こちらの記事を参考にさせていただきました。
参考記事を元に少し丁寧にfetchとmergeで
$ git fetch origin master
$ git merge --allow-unrelated-histories origin/master
Merge made by the 'recursive' strategy.
README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 README.md
うまくmergeできた模様。
ここでファイルをls -laで確認してみるとローカルにもともとあったファイルは消えずに残っていて
リモートにあったREADME.mdファイルが追加されている形になった。
これで$git push origin masterが成功し
この時点でローカルのマスターとリモートが同じになった。
あとはローカルのmasterをfeatureにマージして
ローカルfeature → リモートfeatureにプッシュ。
$ git status
On branch master
nothing to commit, working tree clean
$ git checkout feature
Switched to branch 'feature'
$ git branch
* feature
master
$ git status
On branch feature
nothing to commit, working tree clean
$git merge master
Updating ********
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git push origin feature
これでgithub上でもfeature → masterにプルリクエスト&マージができるようになった。
今回得られた教訓
リソースの無駄はなるべく省く癖をつけよう。
最初の設定時はgithubから作成するか、ローカルからの場合はコマンドでgithub上にリモートリポジトリを作成した方が簡単そう。
構築前にまずgithub連携を行っておいた方が良さそう。
参考URL
githubヘルプ
関連のない履歴が拒否されたエラーの参考にさせていただきました。
Compare & pull requestがタイミングで出てこない場合もあります
これもたまに遭遇しますが以下のページにわかりやすく解説がありました。
参考ページ