LoginSignup
5
1

More than 3 years have passed since last update.

[Updates were rejected because the tip of your current branch is behind] エラー解説、解消法

Last updated at Posted at 2019-11-28

Gitでバージョン管理をしていると、

! [rejected]

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

こんな感じのエラーメッセージとともにpushが拒まれることがあります。

エラーの意味

このエラーは the tip of your current branch is behind この文章にもあるように、

プッシュしようとしている作業ブランチの最新のコミットが、リモートリポジトリの最新のコミットよりも後ろにある

という意味で、

最新のブランチをpullせずに編集してしまったり、
編集の際にgit reset などでどこかしらの過去のコミットの場面に戻って修正してしまっていたときに起こり得ます。

エラー解消法

解消法は2つあります。

git pullで最新の状態のブランチを取得してからpush

まず1つ目は Merge the remote changes (e.g. 'git pull') このヒントメッセージにもあるように、リモートリポジトリにある最新のブランチをpullするという方法です。

git pull (origin 作業ディレクトリ)

一旦最新のリポジトリを最新の状態にしてしまえば、このようなエラーが起きることはありません。

強制push(乱用注意)

非推奨ですが、以下のように今の状態で強制的にpushしてしまう方法もあります

git push -f リモートリポジトリ 作業ブランチ

これは共同開発をしている場合に乱用は避けなくてはいけませんが、 (他の人が、前の状態をpullしてた場合、強制pushの内容をpull出来なくなってしまったりして他の人にも影響が出る可能性もあるので)

1人で開発していて、他に迷惑がかかることがなかったり最新の状態をpullするとコンフリクトを起こすのをわかっていて解消がめんどくさいし不要というときなどに使うのかも

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