From the course: Working with LinkedIn Learning Exercise Files on GitHub

How we use Git branches - GitHub Tutorial

From the course: Working with LinkedIn Learning Exercise Files on GitHub

How we use Git branches

- [Instructor] Each course repository will contain one or more Git branches. This video looks at how we use branches in our exercise files. I'll start with a quick overview of how branches work. Feel free to skip ahead if you already understand the concept. Each repository has at least one branch which contains all the files for the project. Git tracks the history of changes to the branch, so you can see how each file in the branch evolves over time. You can think of a branch as a separate line of development. When working on software, it's common to create a new feature branch when it's time to work on a new product feature. When the branch is created, the contents of the new branch are identical to the original branch. Another common pattern is to create a branch to try out a new idea, or to do a bug fix. Developers can work on a branch in isolation, their commits go into the branch, not the main trunk of the project. Changes in the branch won't affect the main code base and they won't affect other branches either. So you can see here in the feature branch, I've added a new file called update.js, and in the idea branch, I've added a new idea.js file. One of the best parts of Git branches is the ability to instantly switch to other branches. That means I can stop working on my idea branch and start contributing to the feature branch. Later I can go back to the idea branch for more work or delete the branch if the idea is not viable. When the feature or idea is acceptable, the changes can be merged back into the main branch. In this example, I'm merging from the feature branch into the main branch. Once merged, I can remove the feature branch, I can delete idea branches when the experiment is over. Deleting any branch does not affect the main branch. That's a quick overview of branches. Now, let's understand how we use branches in our exercise repositories. For experienced Git users, be aware that we use Git branches in a nontraditional fashion. That's because they are intended to help you learn a topic. When you work on the files, you are following along with the instructor not actively building a new feature for the project. Branches are ideal for code examples. The main differences is that a course can have many branches, many more than a typical software project. For example, on a software project you might have three-to-five active branches. You add branches to create features or to fix bugs. Then those branches are removed as features are tested and merged. The number of branches may vary widely in course repositories. We've got courses with a single branch and courses that have dozens of branches. For example, we've got courses with a branch for every video. And for some of the courses there are two branches per video. One that represents the start state and another that represents the final state at video completion. Having 80 branches in a repo might seem unusual but the advantages are worth it. The instructor can save incremental steps of the examples so you can see the state of the files every step of the way. You select the branch to see the examples that are relevant for that specific video. And remember switching branches is simple and fast. It's comparable to the speed it takes to switch folders in a zip archive. Let's explore some examples in our library. Let's start by looking at a repository that has a single branch. I'm looking at this repository for the bashscripting course. You find the branches on the GitHub website by going to this drop down, this shows you that there's one branch called main, and it's also the default branch. At the root of the repository are a number of README files and LICENSING files. Most of our exercise files go in a sub-folder, in this case, the instructor called it Solution. So let's open this folder and here are the three bashscript files that we'll use in the course. So I click here and I can see the contents of that script file. So in this case, the instructor used it to just distribute three files in a folder to your local computer. In this case, it's not any different than working in a zip file and unzipping it on your hard drive. I took a similar approach for one of my courses. It's here, it's this linq course. Because of the tool I was using, I decided to have a single branch, this is called main. I put my examples in this source folder, but again because I was using this tool called LinqPad, it made more sense to have multiple folders, one per chapter. So I have this chapter six folder, and then each of these files is one that I opened in that video. This is the video one. This is for video two, and these three are for video three. Again, this is not much different than using a zip file. You have a bunch of folders that have files on your hard drive. To really start using the power of branches, you can go to this course called learning-go. Now here we have the main branch but we also have other branches. And our naming convention is to use the chapter number and then the video number, and in some cases we also use the letters b and e, b meaning the beginning state and e being the end state. So let's start by switching over to, I'll go to 02_06b for start at the beginning state and then I'll go look at practice, and then I'll open this main.go file. And here's the contents that the instructor had. David had these at the beginning of the video and he went through and wrote some code, and when he was done, he saved it again in another branch, this one's called 02_06e. So I open up that branch and I see the contents. So this is where we can start getting the power. We can step-by-step go through the course and show you just the content that makes sense for that point in the course. In this video, this is what happened at the end of this video. And we can take this even further. Here's an example in this vue3 essential training course. Same idea, we have a main branch and we have a number of branches. But in this case, the instructor, Ray, he likes to build a project as it goes through the course. So you start with nothing, and then when you finish the course, you have this finished project. So what he does is he merges the changes back into the main branch as it goes through the course. So that means if you go look at main and go look at this source folder, it'll say there's a components folder and there's a views folder, and lots of other files. I'll go back to the root, and now we'll switch to a branch, I'll go to branch chapter two, the first video. And now you'll see that there's only a single file in there. So this is how he starts out the course, and as he goes through the course, you can see the incremental steps but also you only need to open the main branch to see the finished product. So that gives you an idea of how we use branches in some of our courses.

Contents