How to git rebase main/master onto your feature branch even with merge conflicts

September 04, 2021 / 2 min read / 77,734 views, 22 likes, 10 comments

Last updated: September 30, 2022

Tags: git

Git rebase

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

  1. Go to the branch in need of rebasing
  2. Enter git fetch origin (This syncs your main branch with the latest changes)
  3. Enter git rebase origin/main (or git rebase origin/master if your main branch is named master)
  4. Fix merge conflicts that arise however you see fit
  5. After fixing merge conflicts, git add FILE previously merge conflicted files
  6. Enter git rebase --continue (or git rebase --skip if git complains that there were no changes after resolving all conflicts)
  7. Repeat as necessary as merge conflicts arise in the subsequent commits
  8. 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 rebased the main branch onto your feature branch and broken yourself out of rebase hell.

About the author

Theodore Williams

Hi, my name is Teddy Williams. I'm a software developer with a special love for python programming. 🐍👨‍💻 I have a wide range of programming interests including web development, hobby video game development, IoT, data science and just writing scripts to automate everyday boring tasks. I'd love it if you check out some of my other posts or take a look at my portfolio! :)

Thanks for reading this post! 💚 If you like the post, let me know by hitting the icon below, and if you have any questions or comments I'd love to hear them in the comments section. Thanks, and happy coding! 🎉

like post (22) Comments (10)

Log in to comment with your own custom profile. That way you can edit and delete your comments. Plus you can pick out a fun profile picture to show-off next to your comment. 😃🤖👽😻 Not Registered? It's easy! 🤓 Or... fast-comment without a login below (no comment editing/deleting 💩).

User avatar not found

User avatar not found Brey | July 14, 2022 01:24 PM

I'm just registered to write u a thank you message, you saved me from another 8 hours of headache (or more?), thanks man!


User avatar not found verdantfox | July 20, 2022 11:37 PM

Awesome to hear it! Thanks mate! 😀

Vikram | October 03, 2022 10:52 AM

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!


User avatar not found verdantfox | October 03, 2022 01:49 PM

Glad the post was helpful to you. 🙂

Vinayak K | October 28, 2022 11:28 AM

IMO --force-with-lease is a better option in this context than --force


User avatar not found verdantfox | October 29, 2022 12:29 AM

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:

Mert | November 02, 2022 12:24 PM

After a year, I still get back this page and look how to rebase! Thanks man, really thanks :)


User avatar not found verdantfox | November 02, 2022 01:56 PM

I'm so glad it has been helpful for you Mert! Thanks, I appreciate the feedback. :-)

Davis | January 26, 2023 06:08 PM

Thanks! Thought I was gong crazy for a moment before I found this page lol


User avatar not found verdantfox | February 05, 2023 01:38 AM

I'm glad it could help Davis! Thanks for the positive feedback. 🙂