0
0

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 3 years have passed since last update.

git push時のエラー対処法(ローカルリポジトリとリモートリポジトリで差分発生によるエラー)

Posted at

今回もgitについて投稿させていただきます。

エンジニアに転職してまだ2週間のため、まだまだGit、Githubに苦手意識があります…

ブランチの切り替えやマージが怖い…:joy:

そんな中、私が遭遇したエラーについて備忘録として記載しておきます。

エラー内容

[15:25:28]:~/Test-iOS$ git push
Username for 'https://github.com': hogehoge
Password for 'https://hoge@github.com': *******
To https://github.com/hoge/Test-iOS.git
 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'https://github.com/hoge/Test-iOS.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.

ローカルで作業が完了し、git addとgit commit -mを終え、pushしようとしたら上記のエラーが発生し、pushできない…!

原因

実は、pushする前にレビューで不要ファイルがあることがわかり、Githubから削除していました。

image.png
右上のゴミ箱マークからファイル削除していました。

これによって、リモートリポジトリとローカルで差分が発生したため、pushできなくなっていたようです、

解決策

直近のコミットを削除し、git pullすることで差分をなくした後で再度、コミット→pushすることで解決できます。

[15:26:40]:~/Test-iOS$ git log
## 直近のコミットを確認
[15:27:37]:~/Test-iOS$ git reset --hard HEAD~
## 直近のコミットを削除
[15:27:55]:~/Test-iOS$ git log
[15:29:53]:~/BookManager-iOS$ git pull
## リポジトリの差分をなくす。再度、コミット、pushをする

自分はこれで解決できました。普段うまくいっているGitがエラーになると焦りますよね…

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?