はじめに
Github でこんな状態になっています。
これを
とすることができました。
やったこと
本来、remote repository で branch を rebase することは、共同開発者に迷惑をかけるので、良くないことのようです。
が、ここでは個人の担当しているところで、こういうことをしたかったという事情があります。
最初は、このようになっています。
main branch と feature branch があり、main の commit が3つ進んでいます。
feature を main branch に rebase したいという状態です。
今、feature03 branch にいます。
$ git log --oneline --graph --all
* 3b59419 (HEAD -> feature03, origin/feature03) test
| * e8db01d (origin/main, main) main 3rd
| * db94a3a main second
|/
* bca9186 first commit
以下のコマンドでローカルで rebase します。
$ git rebase -i origin/main
Successfully rebased and updated refs/heads/feature03.
そのままpush しようとすると、普通に怒られます。
$ git push origin feature03
To github_torupati:torupati/github-rebase-test.git
! [rejected] feature03 -> feature03 (non-fast-forward)
error: failed to push some refs to 'github_torupati:torupati/github-rebase-test.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 push --force-with-lease origin feature03
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github_torupati:torupati/github-rebase-test.git
+ 3b59419...ab1c45b feature03 -> feature03 (forced update)
冒頭の画像で示すように、Github で repository のbranch が rebase されていました。
できた。。。
以下を参考にさせていただきました。
- https://stackoverflow.com/questions/6199889/rebasing-remote-branches-in-git
- https://qiita.com/niwasawa/items/cc710635b1eee42d8eec
明日も生き延びれそうだ。
(2022/05/18)