DEV Community

Sabbir Siddiqui
Sabbir Siddiqui

Posted on

Git Rebase got you conflicted? Here's what you can do.

Let's say you're working with feature branches. I know Trunk based development is the way to go, but hear me out. If your team is using git flow, this will help you get out of a sticky situation.

If two people are collaborating on the same feature branch, and one person rebases the branch with main/master and pushes to origin, then the other person will face a load of conflicts when pulling the branch from origin (because of the new rebased commits). Here's how you can refresh your local branch completely:

  1. 𝐠𝐢𝐭 𝐟𝐞𝐭𝐜𝐡 𝐨𝐫𝐢𝐠𝐢𝐧 𝐭𝐡𝐞-𝐛𝐫𝐚𝐧𝐜𝐡 (fetch branch from origin)
  2. 𝐠𝐢𝐭 𝐫𝐞𝐬𝐞𝐭 --𝐡𝐚𝐫𝐝 𝐨𝐫𝐢𝐠𝐢𝐧/𝐭𝐡𝐞-𝐛𝐫𝐚𝐧𝐜𝐡 (reset local branch and point to origin)

This will update your local branch with the one in the remote. However, if you already made some changes in your own local branch and the other teammate has rebased it, here's what you can do

Scenario A -> If you already committed the changes, in that case you can just do this:

  1. 𝐠𝐢𝐭 𝐟𝐞𝐭𝐜𝐡 𝐨𝐫𝐢𝐠𝐢𝐧 𝐭𝐡𝐞-𝐛𝐫𝐚𝐧𝐜𝐡 (fetch remote branch)
  2. 𝐠𝐢𝐭 𝐫𝐞𝐛𝐚𝐬𝐞 𝐨𝐫𝐢𝐠𝐢𝐧/𝐭𝐡𝐞-𝐛𝐫𝐚𝐧𝐜𝐡 (rebase your local with the remote)

Scenario B -> If you were just working on some changes but you did not commit them yet, you can simply

  1. 𝐠𝐢𝐭 𝐬𝐭𝐚𝐬𝐡 (To stash your local changes)
  2. follow the initial 2 steps to refresh the local branch
  3. 𝐠𝐢𝐭 𝐬𝐭𝐚𝐬𝐡 𝐩𝐨𝐩 (to get your local changes back)
  4. continue your work!

gitflow #trunkbaseddevelopment #softwareengineering #softwaredevelopment #git #github #versioncontrol

Top comments (0)