Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The last major piece of functionality we need to explore is branching and merging. In Git, branching and merging is very easy and very lightweight. So first, let's run Git branch. We can see we are on the master branch. This is sometimes called the trunk. To create a branch is as easy as saying git branch b1, and now we have a new branch, you can see we have branches b1 and master. And you can see between the asterisk and the text in green, they we're on the master branch.
So in order to make changes in the branch and work with that, we need to check that branch out and then we can make updates. So git checkout b1, I can see it says we switch to branch b1. If we do git branch, you can see we are on branch b1, so now we'll go and update our file and add a new feature. One more haiku. "No available space," "Look to the cloud," "Your drive is full".
I will save that, we will exit, and then we'll commit that in the branch. Now if we say git log, you can see that we have the Update in branch, and you can see we have changed my novel to website, and we can see we have Initial check in. But there is nothing there that makes it clear that the update was done in the branch because the set of revisions is global between all the various different branches. So let's make another change, and we'll go specifically and make the commit message to show what is what we want to do.
So we'll say Notepad f1.c, and we'll come down here and change the word Website to Node, and save that, and we'll quit and then we'll do another commit, and we'll say b1 Change website to node. And there's a convention that's just handy to know which branch you are making the changes in. So now if we say git log, you can see it's very clear that that change was made in branch number one.
All right. Now let's talk about merging. If you continue to work in a branch for an extended period of time, you'll want to do a merge from your main branch back into your private branch. Remember, that's called a forward integration. Time to do this varies depending upon how active the main branch is. So usually I do that weekly if I am working in a project with somebody so that I make sure that my private branch is up to date in all the places where I am not currently working. Now in our case, we are simply going to do a reverse integration from our branch b1 back into the master.
So the way you do this is you first switch back to the master, and if we type f1.c, you can see we don't have our new feature, and it still says website. Now we are going to merge, git merge b1, and now if we type out f1.c, you can see it not only has the word Node, it also has our second function, feature1. You'll see one thing up at the top there where we did the git merge b1, it says Fast-forward, and that means that Git recognizes that b1 came off of a common starting point with our master branch, so therefore, it was able to replay the changes very, very quickly by knowing that it didn't have to like start looking at everything because it knew where they both began to differ.
Well, that's it for branching and merging in Git. Next up, we'll take a look at some shell integration and some GUI tools for Git that were installed when we installed the Git installation that we got from git-scm.com.
Get unlimited access to all courses for just $25/month.Become a member