Does your project prefer git rebase
instead of git merge
? Has your branch
fallen out of sync with the main
branch and you are unable to automate
your rebase due to conflicts? If so, you might have run into rebase hell.
This happens when you try to git rebase
, solve your conflicts, and push to the
main
branch, only to find that the main
branch is now, once again, out of
sync in a never-ending loop. Letās break out of rebase hell with
this short guide to rebasing.
The steps
- Go to the branch in need of rebasing
- Enter
git fetch origin
(This syncs your main branch with the latest changes) - Enter
git rebase origin/main
(orgit rebase origin/master
if your main branch is namedmaster
) - Fix merge conflicts that arise however you see fit
- After fixing merge conflicts,
git add FILE
previously merge conflicted files - Enter
git rebase --continue
(orgit rebase --skip
ifgit
complains that there were no changes after resolving all conflicts) - Repeat as necessary as merge conflicts arise in the subsequent commits
-
Once the rebase is complete, enter
git push origin HEAD --force
This command pushes your rebase fixed branch to
remote
. The--force
is important as it tells remote, āI know these changes are correct, accept them as is.ā Without the--force
flag, your remote branch will continue to believe it is out of sync and will claim a merge conflict.
And thatās it. You have now git rebase
d the main
branch onto your
feature branch and broken yourself out of rebase hell.
Comments (27)
Brey
July 14, 2022
Iām just here to write u a thank you message, you saved me from another 8 hours of headache (or more?), thanks man!
Teddy Williams
July 20, 2022
Awesome to hear it! Thanks mate! š
Vikram
October 03, 2022
Lovely post! You made my day. I always used to do āmergeā and ārebaseā is new to me and this helps a novice user for ārebaseā. Though I found a similar post and guide, step-7 in your post āRepeat as necessary as merge conflicts arise in the subsequent commitsā helped to understand more clearly that ārebaseā is something to deal with each commit Iāve in my feature branch and just like āmergeā that does everything-at-once!
Teddy Williams
October 03, 2022
Glad the post was helpful to you. š
Vinayak K
October 28, 2022
IMO
--force-with-lease
is a better option in this context than--force
Teddy Williams
October 29, 2022
Interesting. I hadnāt heard of
--force-with-lease
. Looking into it, I think you might be right. In most cases it wouldnāt change anything, but if you happened to have multiple developers working on the feature branch at the same time it could save some headache. Hereās a stack overflow post about the difference for anyone reading this interested: https://stackoverflow.com/questions/52823692/git-push-force-with-lease-vs-forceMert
November 02, 2022
After a year, I still get back this page and look how to rebase! Thanks man, really thanks :)
Teddy Williams
November 02, 2022
Iām so glad it has been helpful for you Mert! Thanks, I appreciate the feedback. :-)
Teddy Williams
January 05, 2023
Iām glad it could help Davis! Thanks for the positive feedback. š
Davis
January 26, 2023
Thanks! Thought I was gong crazy for a moment before I found this page lol
Sardor
October 10, 2023
thanx man , helped a lot
Teddy Williams
October 10, 2023
Youāre welcome Sardor, Iām glad it helped. š
Anton
November 02, 2023
Hey, this was very simple and nice, in our day-to-day life we usually work on our feature branch and then we regularly need to rebase as it is sometimes quite common among developers. But at the same time, it is confusing to developers who are used to merging rather than rebasing. So for those group, this is very easy and simple to follow. Thanks.
Amr
November 26, 2023
Thanks Bro <3
GeorgeM
November 30, 2023
Hi Teddy, my main branch is master on our production server. I have merged all development changes on the master branch on GitHub and resolved all conflicts. Will pulling master from GitHub to the production server simply overwrite master on production or will I have the same 200 plus merge conflicts to resolve all over again?
Teddy Williams
November 30, 2023
If I understand the question correctly, itāll overwrite without new merge conflicts. As long as you havenāt made local changes to your main branch (ie there are extra commits on your serverās main branch compared to the remote source), I believe pulling the main branch will always sync your local (server) main branch with the HEAD of the remote main branch.
GeorgeM
December 01, 2023
Thanks Teddy, thatās what I thought would happen with the merge from GitHub to our production server. This is a major rewrite, necessitating an outage while installing the new site. I want to keep the outage window as short as possible.
Christian Galaz
January 08, 2024
Muchas gracias por tu aportaciĆ³n, me has quitado muchos dolores de cabeza
Teddy Williams
January 08, 2024
De nada. Me alegro de poder ayudar.
Carla
January 17, 2024
Thank you for the explanation comments next to the commends, thatās vital info!
Mots
February 20, 2024
Can you quote examples on how to merge conflicts with theirs vs ours if Iām re basing master on my feature branch. It always confuses what is ours and theirs and I read somewhere the context changes in rebase vs merge.
Teddy Williams
February 20, 2024
Interesting, I didnāt realize there were differences in āoursā vs. ātheirsā in rebase vs. merge. Sorry, Iām not going to go into details on solving merge conflicts for this post. I hope you can sort out the problem, though. Best of luck.
Eva
March 19, 2024
Thank you! Finally a clear easy path to do this thing Iāve struggled to do correctly for years.
Teddy Williams
March 20, 2024
Iām so glad the post was helpful for you! :)
Wenny
March 22, 2024
Thank you for this clear and concise post! It definitely got me out of the rebase hell - now Iām not afraid of rebasing anymore š¤
Teddy Williams
March 22, 2024
Iām glad it was helpful, Wenny! š
K
April 16, 2024
Nice
Write a comment
You can style your comment using markdown. See this markdown cheat sheet for ideas. Comment as a guest with the below form, or to comment as yourself. Optionally, provide your email to be notified when others comment on this blog post.