Suppose that I have 2 branches: v1.0 and development.
Our process is to create local branch using:
git merge-base v1.0 development
git checkout <commit-hash>
git checkout -b <new-branch-name>
Suppose that one of my colleagues follows the same process and made changes recently by:
git checkout v1.0
git merge <his-local-branch-name>
git push
git checkout development
git merge <his-local-branch-name>
git push
My question is, how can I easily update my local branch with his recent changes?
What I did, was to create another branch with recent changes using merge-base and merge it with my changes made locally.
But do it exist some easy way? I was thinking about something like git merge <last-commit-hash> but it generates lots of conflicts.
Okay... so it sounds like
developmentis a long-lived branch representing the previous release(s) - much likemasterin gitflow.And it sounds like
v1.0is a long-lived branch where you're assembling the next release, much likedevelopin gitflow.And a given local branch might be like a feature branch (in which case you'll merge it to
v1.0) or like a hotfix (in which case you'll merge it to bothv1.0anddevelopment). The odd thing is that you always seem to create the local branches so that they could be merged to both. (So do you not know, at branch creation time, whether you'll be merging todevelopment? Because if that's not the case, making every branch "start over" at the merge base seems like it has some unnecessary merge resolution costs... But I digress.)Let's step through your scenario with pictures. You start with
and you create a local branch
and your coworker creates a local branch
(Bear with me here; I realize that there may never be any one repo with all of these branches in it, but let's just look at the "big picture" anyway...)
So your coworker merges to both long-lived branches
Note that from this point forward,
Ois the merge base fordevelopmentandv1.0. But your branch was created when the merge base wasA, so now we reach your question: how to gethotfixinto your branch.By now
hotfixis an integral part of the shared history, so you probably don't want to do anything that rewrites and/or duplicates the changes from its commits.You likely don't want to merge
v1.0into your branch, because mixingZinto your branch would seem to run against the grain of having created the branch at the merge base.So you really just want to combine
Ointo your branch. Now let's switch gears a bit and look at how your local repo might see things, if I take your term "local branches" literally (meaning you don't have thehotfixbranch):Now, given that
featureis also local (only present in your repo), one option is to rebase it to the new merge base - and this seems like it remains in the spirit of your workflow.would give you
At this point
B'andC'are both untested states of the code. Ideally you should test both of them and address any issues (though, if there are issues inB'that's easier said than done), so that you will still have a clean history.The other option, which would avoid the "untested commits" issue but create a "messier" (though arguably more accurate) history, is to simply merge the merge-base into your branch.
Which gives you something like
Which, after taking the long way around to say "why", means we did essentially what you already did, except skipping the step of creating a branch for the merge because we can just refer to the merge base directly.
And that makes sense. You'd already figured out what changes to combine with your branch - you don't really want to change what commit you're merging. So the best we can do is find a simpler way to refer to those changes.