このメモでは、git の subtree で push しようとしたら
error: failed to push some refs to (リモートの名前)
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.
のエラーが出て、pull してから push しても同じエラーが出て困ったときに使えた方法を紹介します。
動作環境
もしかしたら環境の違いで結果が変わるかもしれませんので、一応環境も載せておきます。
- OS: Windows 10 上の Virtual box の ubuntu 16.04 LTS
- git のバージョン: 2.7.4
症状
普通に
git subtree push --prefix=(ディレクトリ) (リモート) (ブランチ)
で push すると
error: failed to push some refs to (リモートの名前)
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 subtree pull --prefix=(ディレクトリ) (リモート) (ブランチ)
しても
From (リモート)
* branch (ブランチ) -> FETCH_HEAD
Already up-to-date.
と表示され、そのあとまた push しても同じエラーメッセージが出て push できない。
解決法
https://stackoverflow.com/questions/13756055/git-subtree-subtree-up-to-date-but-cant-push
https://stackoverflow.com/questions/33172857/how-do-i-force-a-subtree-push-to-overwrite-remote-changes
の情報を基に
git push (リモート) `git subtree split --prefix=(ディレクトリ) --onto=(リモート)/(ブランチ)`:(ブランチ)
としてみたら push してくれました。
なお、このコマンドでもダメな場合、自己責任でこのコマンドの最後に--force
を付ければ無理やり push することもできます。
あとがき
subtree を使い始めてから数日に一回はこの解決法に助けてもらっているのですが、どうにかならないものでしょうか…。