LoginSignup
16
15

More than 3 years have passed since last update.

【GitHub】はじめてのgit push がうまくいかない時の対応

Posted at

はじめに

はじめてGitHubに触れ、リモートリポジトリへの接続、pushを行った際に自分がつまづいた点を記載します。
初学者ゆえに基本的な内容になっているかと思いますが、同じエラーが発生した方のご参考になれば幸いです。
バージョン : git version 2.21.1

前準備 GitHubへの登録・SSH接続

GitHubのユーザー登録を行い、SSH接続の設定を行う。
SSH接続については、主に「GitHub.com ヘルプドキュメント」を参考にしながら進めました。
またssh-agentの理解については「ssh-agentを利用して、安全にSSH認証を行う」が参考になりました。
現段階では秘密鍵はGitHubで使うだけですが、複数サーバーに跨がる場合に真価を発揮するよう。

リモートリポジトリの設定

GitHubでのリポジトリ作成後、そのリモートリポジトリ情報をターミナルで追加。(リモートリポジトリを紐づける)

git remote add origin https://github.com/****.git

リモートリポジトリにpush

設定したリモートリポジトリにpushするところでエラー発生。

$ git push origin master
To https://github.com/****.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/****.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.

「ローカルにないworkがリモートに含まれている」
「git pullによりその違いを統合してから再度pushするように」といった内容。

git pullを行なったが、うまくいって・・・ない。

$ git pull origin master
From https://github.com/****
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

mergeができていないようだ。

試しに再度git pushしてみたところ、、、

$ git push origin master
To https://github.com/****.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/****.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

やはりmergeされていない(git pullがうまくいっていない)ことが問題のようだ。
参考:「non-fast-forward エラーの扱い」

そして試行錯誤の末、下のログにいきついた。

$ git pull origin master
error: Pulling is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

「mergeされていないファイルがあるからpullできない」
「ワーキングツリーでそれらを構築し、git addしてcommitせよ」といった内容。

$ git add -A
$ git commit
git pull origin master
From https://github.com/****
 * branch            master     -> FETCH_HEAD
Already up to date.
$ git push origin master

これで成功しました!

考察① git pullがうまくいかなかった理由

「Gitのマージ概要および、共通の分岐元を持たないブランチ同士のマージ(割としょうもない話)」に書いてありました。
「共通の祖先コミットが無いコミット同士ではマージをすることはできません」とのこと。
もともとローカルで作成していた内容とリモートで新たに作成した内容(READMEファイルが自動生成)は
共通の祖先コミットがないため、今回現象に至ったようです。
そういった場合は(かなりイレギュラーな対応で今後やることはないでしょうが)
「--allow-unrelated-histories」オプションを付けることによりmergeできる。

考察② git addとgit commitでうまくいった理由

とてもわかりやすい記事「git fetchの理解からgit mergeとpullの役割」によると、
「pullはfetchとmergeの両方を組み合わせたコマンド」。
今回git pullではあくまでmerge工程までうまくいかなかっただけで、fetchについては実行がされていた。
その状態でgit addとgit commitを実施してリモートとの差をなくす(ローカルのREADMEファイルを更新する)ことにより、最終的にgit pullがうまくいった。

まとめ

色々と遠回りしましたが少しGitHubの理解が進みました。
また上記考察は僕の推定が多分に含まれておりますので、
誤り・認識違い等ありましたらコメントでご指摘ください。

16
15
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
16
15