0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

git push origin時の「error: failed to push some refs to 〜」について

Posted at

はじめに

業務上githubを使用する機会が増えてきたため、まずは初歩的なコマンドから確認するため、ローカルリポジトリからリモートリポジトリにpushを行っていました。
すると以下のエラーが出力されたため、原因の確認とそれに対応する対処法をまとめました。

事象

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

先に原因

ローカルのmainブランチがリモートのmainブランチよりも古いため、Gitが拒否している状態であるため、pushができなかった。

先に対処法

リモートの変更を取り込んでから再度プッシュする必要がありました。

git pull origin <branch name> --rebase  # リモートの変更をローカルに適用
git push origin <branch name>  # 変更をプッシュ

なぜエラーになったのか?

実は今回ファイルをpushする前に作業ブランチをスイッチしておりました。
現在進めているブランチとは異なるブランチで新しくpushしてしまった結果、リモートとローカルの履歴が異なり、エラーが出力されたと考えています。

他にエラーが発生する原因として検索してみると以下の事象も考えうるとのことでした。

•	リモートに変更が加わったが、ローカルに取り込んでいない
•	ローカルのmainが古く、リモートの最新のコミットを知らない
•	その状態でローカルの変更をプッシュしようとすると、Gitは「データの損失を防ぐため」拒否する

以上です。引き続きgitについて理解していきます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?