0
1

More than 3 years have passed since last update.

gitのエラーUpdates were rejected because the remote contains work that you do not have locally. 〜the remote changes (e.g., 'git pull ...') before pushing again.の解決法

Posted at

前提

・現在ブランチAにいる。
・ブランチAでgit add、git commit -m"メッセージ"、git push origin ブランチAをした。
・git checkout masterをし、masterブランチでgit merge ブランチAをした。
・リモートのmasterブランチに反映させるために、masterブランチにいる状態でgit push origin masterをしたところ、次のように表示された。

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.

原因

the remote contains work that you dohint: not have locally.
→リモートはあなたがローカルで持ってないworkを持っている。

つまりリモートリポジトリ(github上のリポジトリ)のmasterブランチには存在するのに,ローカルのmasterブランチには存在しないcommitがあるということ。

「リモートリポジトリのmasterブランチにはコミットhogehogeがあるのに、なんでローカルのmasterブランチにはコミットhogehogeがないの?おかしくね?」と言われています。

手順

・masterブランチにいる状態でgit checkout -b ブランチBをすることによって、git pull origin masterをする用のブランチBを作成し、ブランチBに移動する。
・git pull origin masterをしてリモートリポジトリのmasterブランチをローカル環境のブランチBのなかにpullする。

・するとローカル環境がリモートリポジトリのmasterブランチの内容に変わる(ブランチ名はブランチBとなっています)のでいったん本当にこれをローカルのmasterブランチにpushして良いのかを確認する。

・オッケーだったらgit add、git commit -m"メッセージ"、git push origin ブランチBをする。

・そしてgit checkout masterをしてmasterブランチに移動し、git merge ブランチBをする。

そしてマージした内容をリモートリポジトリのmasterブランチに反映させるためにgit push origin masterをする。

以下、手順の部分をコマンドに置き換えました。

$ git checkout -b ブランチ2 #masterブランチにいる状態で行ってください!

$ git pull origin master

$ git add -A

$ git commit -m"メッセージ"

$ git push origin ブランチB

$ git checkout master

$ git merge ブランチB

$ git push origin master

ポイント

リモートリポジトリのmasterブランチを反映させるためのブランチを作成することが大事。記事中だとブランチBがその役割。
開発中においてテストブランチの方ではな、直でmasterブランチにgit pull origin masterするのは危険です。

0
1
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
0
1