Join Michael Lehman for an in-depth discussion in this video Branching and merging, part of Learning Software Version Control.
Let's explore branching and merging in Hg. In Hg, branching and merging is very easy. First, let's run hg branch and see that there is only one branch default, and that's the one we're on. Sometimes it's called the trunk. But in Hg, it's called the default. So to create a branch, we say hg branch b1. Now notice it says mark the working directory as branch b1. It hasn't actually done the branch yet, so we have to commit it to make the branch. Okay, now if we say hg branch, you can see we're on branch b1, and if we see hg branches, you can see there are two branches, b1 and default.
Now to work with a branch we simply update to the branch, hg update b1, and we ask hg branch it tells we're on b1, and if we say hg update default and we type hg branch, we're back on the default branch. So let's go back to branch b1 and make a change. So we'll add another feature to our little haiku up here "No more available space," "Look to the cloud," "Your hard drive is full!" Now if we save this and commit it--and remember now we can say hg branch--and see that we're in branch b1, type out f1.c, and see that we've got the new feature. If we go hg update default, type out the f1.c, you see we just have the original values.
I'll go back to the branch, I will look at the log, and you can see that it shows us in the list which branch it was in. So it was in branch b1 that we did the update. Like Git, however, it's a good convention in your summary to indicate which branch the changes are in. So now we're going to go make one other update, and we're going to change website to node and then we'll commit that, and we'll put the branch number in front of it, so we know exactly where we did, what we did.
All right, so now let's talk a little bit about merging. If you continue to work in a branch for an extended period of time, you'll want to do a forward integration from the default back to your private branch. Remember back in the conceptual section we talked about forward integration and reverse integration? Forward integration is that process of taking things from the main or the trunk or the default and moving them into your private branch. The purpose of doing this is to make sure that your private branch doesn't get too far out of date with the main, if you have other people working on the main branch so that you're collecting together updates perhaps in sections of the application that are different than the one you are working in.
Now in this case, we don't have anything useful in our main branch to bring forward, so we're just going to do a reverse integration. Remember, a reverse integration is taking changes from our private branch and putting them back in the main. In order to merge, we need to switch back to the master branch or the default branch. So we're going to say hg update default, we can see we just have our original data, and then we're going to ask it to merge branch b1 get back into the default branch. And notice it says here, again, branch merge, don't forget to commit.
If we see hg status, you can see that not only does it have modified file f1. It also has this file it doesn't know what to do with code f1.c.orig, and you should just leave that alone, and in this case, we're going to do the commit, and if we do hg log, you can see that now we have our updated from branch b1, and that's it. Now we type f1.c, you can see we have all our changes from the branch merged back into the default, we can verify when the default branch by saying hg branch and see that we're in the default branch.
So that's branching and merging in Hg. Next step, we'll take a quick look at looking at GUI tools and shell integration for Hg.
- Comparing centralized vs. distributed systems
- Saving changes and tracking history
- Using revert or rollback
- Working with the GUI tools
- Using IDE and shell integration
- Installing different systems
- Creating a repository
- Tagging code
- Branching and merging code
- Selecting a software version control system that's right for you