Lesson 4-3

How do I Branch on a Repository?

Like I said earlier, we need to do 3 things in order to Branch.

  1. Create a Branch to work out of (you can also do this on GitHub.com)
  2. Add code to that branch
  3. Commit/Push the Branch

For these instructions, I will show you how to do all 3 steps in each sub-section.

GitHub Desktop

Creating a Branch
  1. Open GitHub Desktop and open the repository that you want to branch in.
  2. On the top ribbon under "Current Branch", click the button in that area.
  3. Click "New Branch".(see Figure 4A)
  4. new Branch and all other branches Figure 4A: This window shows all of the Branches you have, as well as it gives you the option to make a new one, and filter the ones you already have.
  5. Enter a name for your new Branch and then click "Create Branch".(see Figure 4B)
  6. new branch pop up window Figure 4B: This is what the new branch window will look like.
  7. On the main screen, click "Publish Branch". This will push the new branch to your repository on GitHub.com.(see Figure 4C)
  8. branch has been created Figure 4C: This is what you shall see once you have created a branch.

To switch between branches, follow step 2, but instead, select the branch you want to work out of.

This process is called git checkout and works similar to checking out a book from a library. You are taking all of the files from a specified branch and working inside of them at a different location in your repository, like how you take a book home from the library to read and then eventually return.

Note that it will say for you to do a pull request after the last step, but that is optional. It allows you to compare two branches (the one you created and an already existing one).You can, as will I, ignore this.

Now go ahead and do any coding you want. In my case, I added a file called hello.py that prints "helloWorld" in the console.

Committing/Pushing with a Branch

There really isn't much to say here. Just like you would commit/push on the main branch from 3-3, you would follow the same steps but instead you are committing to your new branch.

After committing/pushing this is how the main branch looks:

main branch on github.com Figure 4D: Notice that "hello.py" isn't to be found here.

...and this is how our helloWorld branch looks:

helloWorld branch on github.com Figure 4E: Notice that "hello.py" is here. That is the magic of branching (not Macy's).

Now that we have added some code, let's merge our branch back into main. You would normally do this once you are ready to merge new changes from the front-facing part of your app with the new backend code.

Merging 2 Branches
  1. Open the branch you want to merge into in repository. (In most cases, you want to merge a separate branch into your main branch.)
  2. On the top ribbon under "Current Branch", click the button in that area.
  3. You should see all of your branches, including the main one. Click "Choose a branch to merge into main".(see Figure 4F)
  4. selecting branch to merge in github desktop Figure 4F: You will be presented with all of the branches in the repository.
  5. On the Pop up window, select the branch you want to merge and click "Merge \branchName\ into main".(see Figure 4G)
  6. merging 2 branches in github desktop Figure 4G: Select the branch to merge and click the button at the bottom.
  7. Push your new changes.

That is all that there is to doing branching on GitHub Desktop. Now let's explorer how to do it in Terminal.

Terminal

Creating a Branch
  1. Navigate to the directory with your repository via the cd command.
  2. To create a new branch, type in git branch \branchName\ and hit enter.(see Figure 4H)
  3. creating branch in terminal Figure 4H: This is what you will see after you click enter. Nothing will pop up, but that is normal.
  4. Just like in terminal, you can make a directory (mkdir) but it doesn't put you inside of it. In order to make changes in the newly created branch (or even to switch between branches) type git checkout \branchName\ and then click enter.(see Figure 4I)
  5. switching to newly created branch in terminal Figure 4I: Simple command to switch to new terminal. Read the output to make sure you're in the right branch!

    Now go ahead and add some code to this branch. Once that is done, you can go ahead and push it to GitHub.

  6. Once you are done with adding at least 1 file, type in git push -u origin \branchName\ and click enter.(see Figure 4J)
  7. pushing newly created branch in terminal Figure 4J: Pushing the branch is similar to doing so normally.
  8. The previous step only pushed the new branch. Go ahead and commit/push new changes as outlined in section 3-3.



Similar to before, you might need to switch between branches. You can do so by typing git checkout \branchName\

If you want to see what branch you are in currently, type in git branch. The one with the * next to it will indicate where you are. This command will also list all branches in the repository.

Again, notice how main does not have our hello.js, but Stage01 does:

pushing newly created branch in terminal Figure 4K: hello.js is nowhere to be found in main. pushing newly created branch in terminal Figure 4L: hello.js is alive and well in our branch named 'Stage01'.

We are now ready to merge back into main. Here is how we do just that.

Merging 2 Branches
  1. Move to the branch you want to merge into by typing git checkout \branchName\ and clicking enter.(see Figure 4M)
  2. checking out of new branch into main Figure 4M: We have left our new branch to get ready to merge.
  3. Now, to merge the two branches, type git merge \branchThatYouWantToMergeWithMain\.(see Figure 4N)
  4. merging new branch into main Figure 4N: You should see something similar once you run the merge command.
  5. Make sure everything transferred over by typing ls into terminal. Use nano \file.extension\ view the contents of a file as a testing point.
  6. Push changes to GitHub like normal.(git push)

Some things to note about branching

  1. Branching will merge all commits made in an individual branch with those of the parent branch.
  2. All files and folders are transferred over and overrides anything already in the new branch.
  3. The Branches, themselves, aren't deleted when merged. To delete them you can type git branch -d \branchName\.

So there you have it. Branching isn't all that bad. Hopefully you better understand the way Branching works. I sure do. Let's recap what we learned on the next page.