8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

githubでCompare & pull requestボタンがなかった時

Last updated at Posted at 2019-11-25

始まりはgithubでプルリクが出せない!?
というものでしたが色々絡んでいて少し大変だった時の記録です。

前提、状況

今回新規でwordpressサイト立ち上げ時にローカルで開発して
featureブランチでプッシュ、そのあとmasterに
最初のプルリクエスト時にタイトルのメッセージが表示された。
そしてCompare & pull requestのボタンも見当たらないという状況。

エラーメッセージ

error.sh

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のボタンは出たものの新たなエラーメッセージが

error2.sh

There isn’t anything to compare.
master and feature are entirely different commit histories.

とりあえずローカルのmasterブランチを最新にして
ローカルmaster → リモートmasterのプッシュを試みる

mergefromfeature.sh

$ 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から手動でディレクトリごとバックアップコピーを取っておく。

pull.sh

$ 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で

fetchandmerge.sh

$ 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にプッシュ。

finish.sh

$ 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がタイミングで出てこない場合もあります

これもたまに遭遇しますが以下のページにわかりやすく解説がありました。
参考ページ

8
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
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?