1

Looking for a best practice here. Our process is that we create a new branch from master

master --> myBranch

make our updates, then push to dev

myBranch --> dev

Obviously, sometimes we have merge conflicts on dev. In that situation, we we checkout our branch via:

git fetch origin myBranch
git checkout dev
git merge FETCH_HEAD

we then resolve our conflicts and...

git push origin HEAD

My understanding of the above is that I'm merging myBranch into dev manually, and then pushing my local dev changes to remote.

That's usually the end of it.

But now I need to make further updates to myBranch and push to dev and that's where I'm stumped. Won't I now (likely) get the same conflicts as before? What's the best practice here? Should I be doing a git pull origin dev to update my own branch to match dev?

DA.
  • 39,848
  • 49
  • 150
  • 213

1 Answers1

2

Short answer

Use this sequence to pull the remote data and update your branches:

git checkout myBranch
git pull

git checkout dev
git pull

git merge myBranch
... resolve conflicts ...
git push HEAD


There's a chance that similar conflicts will happen again in future if myBranch will edit the same parts of files again. The conflicts, though, will never be the exactly the same. If you still want to avoid that - you need to push your merge commit to myBranch (but note that it will bring all dev changes to myBranch):

git checkout myBranch
git merge dev
... this is a Fast-Forward merge ...
git push HEAD

Long answer

Your understanding is right:

... I'm merging myBranch into dev manually, and then pushing my local dev changes to remote.

and this is almost correct too:

Should I be doing a git pull origin dev to update my own branch to match dev?

So, yes - the current approach indeed merges myBranch into dev manually, and then dev is pushed to the remote repository. And yes - it's better to update your local dev first.

There are a couple of assumptions that are not that correct, and it's great that you have created an SO question to verify them.

First of all - git pull origin dev is not a universally right command to update your local branch dev. What it does is pulling the remote devchanges, but it updates your local dev branch only if you're on it already. Seems like you're not - because in the provided example you check that branch out before merging FETCH_HEAD. So the suggestion is either to check out the branch before pulling its changes:

git checkout dev
git pull origin dev

git fetch origin myBranch
git merge FETCH_HEAD

git push HEAD

or use a different command to both get the changes and update your local branch (you must not be on that branch currently):

git fetch origin dev:dev
git checkout dev

git fetch origin myBranch
git merge FETCH_HEAD

git push HEAD

But probably more simple is just to pull all the updates, and then merge the branches you need:

git checkout dev
git pull
git merge origin/myBranch

git push HEAD

However all the code snippets above leave your local myBranch not updated with the changes done in remote repository, which is inconvenient for further work. So the best practice is to update both local branches before merging them:

git checkout myBranch
git pull

git checkout dev
git pull

git merge myBranch
git push HEAD

Second:

Should I be doing a git pull origin dev to update my own branch to match dev?

This is needed not just for merging, but for your ability to push changes back. If you won't update your local dev, while remote has some changes in dev, then you literally won't be able to push your merge commit to the remote repository, and you will need to resolve even more conflicts first.

Third:

I need to make further updates to myBranch ... Won't I now (likely) get the same conflicts as before?

This is possible, though those won't be exactly same conflicts. To get them myBranch will need to modify the parts of code that you changed in dev (including in that merge commit). Modifying code in other files or even in other parts of the same file won't produce the conflicts. To avoid the conflicts you'd better sync myBranch with dev as well (if you're ok to see other dev changes getting to myBranch):

... do all the pull-merge-push stuff as suggested above ...

git checkout myBranch
git merge dev
... this is a Fast-Forward merge ...
git push HEAD
Andrey Tserkus
  • 3,644
  • 17
  • 24
  • Thank you so much for this! That last bit answer the big question I had...wanting to update my local MyBranch to match Dev to avoid further conflicts with future merge requests. – DA. Apr 12 '19 at 02:25