Sign In

Forgot your password? Request password reset...

Don't have an account? Register here...

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

September 04, 2021 / 2 min read / 155,514 views 30 likes 27 comments

Last Updated: May 12, 2024

git

Rebase diagram

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

Teddy Williams (author) smiling with mountains in the background

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 tedious 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 heart/like icon below. Share this article if you found it interesting, and if you have any questions or comments, I'd love to hear them in the comments section. Thanks, and happy coding! šŸŽ‰

·

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's profile picture

Teddy Williams

July 20, 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!

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's profile picture

Teddy Williams

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ā€¦

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's profile picture

Teddy Williams

October 29, 2022

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

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-force

Mert

November 02, 2022

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

Teddy Williams's profile picture

Teddy Williams

November 02, 2022

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

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

Teddy Williams's profile picture

Teddy Williams

January 05, 2023

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

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's profile picture

Teddy Williams

October 10, 2023

thanx man , helped a lot

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's profile picture

Teddy Williams

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ā€¦

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

If I understand the question correctly, itā€™ll overwrite without new merge conflicts....

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's profile picture

Teddy Williams

January 08, 2024

Muchas gracias por tu aportaciĆ³n, me has quitado muchos dolores de cabeza

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's profile picture

Teddy Williams

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ā€¦

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's profile picture

Teddy Williams

March 20, 2024

Thank you! Finally a clear easy path to do this thing Iā€™ve struggled to do correctly for years.

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's profile picture

Teddy Williams

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 šŸ˜¤

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.