今回もgitについて投稿させていただきます。
エンジニアに転職してまだ2週間のため、まだまだGit、Githubに苦手意識があります…
ブランチの切り替えやマージが怖い…
そんな中、私が遭遇したエラーについて備忘録として記載しておきます。
エラー内容
[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から削除していました。
これによって、リモートリポジトリとローカルで差分が発生したため、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がエラーになると焦りますよね…
参考文献