Search code examples
gitbitbucketgit-workflow

How do I properly use git workflow with rebase?


I am using bitbucket and am running into problems using the rebase feature of git. In short I need to reapply changes I already applied each time I rebase. I have recreated the problem using the steps below. Its a simplified version but the outcome was the same.

  1. Developer A creates a git repo and adds a contributors.txt to master, commits and pushes. Now master has a contributors.txt file. The contents of the file is Developer A
  2. Developer A creates branch-a from master.
  3. Developer B creates branch-b from master.
  4. Developer A appends the contributors file in branch-a so it looks like. Developer A Developer A2
  5. Developer B appends the contributors file in branch-b so it looks like Developer A Developer B
  6. Developer A commits and pushes the changes to remote branch-a
  7. Developer B commits and pushes the changes to remote branch-b.
  8. Developer A merges branch-a with master.
  9. Developer B checks out master and pulls so the local master gets updated with the changes applied by the merge mentioned in step 8 above.
  10. Developer B checks out branch-b and does git rebase master
  11. Developer B gets a merge conflict.
  12. Developer B fixes the merge conflict so the file now looks like Developer A Developer A2 Developer B
  13. Developer B does git add to add the fixed conflict and runs git rebase --continue. All goes well.
  14. Developer B pulls branch-b since now the local and remote branch-b has diverged they need to be merged. (Needs to set-upstream before pull)
  15. Developer B resolves merge conflict, now the local branch-b has the changes from master and can be pushed to remote.
  16. Developer B pushes branch-b to remote. using git push origin branch-b. All goes well.
  17. Developer B creates a new file in branch-b and commits.
  18. Developer B then does
    • git checkout master
    • git pull (master is up to date since it has not changed since last pull i.e. Step 9)
    • git checkout branch-b
    • git rebase master
  19. When git rebase master is executed, git asks developer B to resolve the same conflicts all over again. Why does this happen? Shouldn't the already applied changes be known and not asked to be applied again?

I think I can use merge instead of rebase to overcome this problem. But it seems like I am using rebase incorrectly. Please let me know what I am doing wrong


Solution

  • After step 13, you should be able to do a git push origin branch-b --force. This will push the rebased version to branch-b to your remote, and because it's been rebased on top of master, you should then be able to merge with master without any issue.

    The issue with the previous method is that after rebasing on top of master, you're just adding the merge commit to origin/branch-b which hasn't been rebased on top of master so it's still diverged. If you do a force push, then it just replaces the remote branch with your rebased branch.