Skip to content

lab 21 Creating a Branch

Goals

It’s time to do a major rewrite of the hello world functionality. Since this might take awhile, you’ll want to put these changes into a separate branch to isolate them from changes in main.

Create a Branch

Let’s call our new branch ‘greet’.

Execute:

git checkout -b greet
git status

NOTE: git checkout -b <branchname> is a shortcut for git branch <branchname> followed by a git checkout <branchname>.

Notice that the git status command reports that you are on the ‘greet’ branch.

Changes for Greet: Add a Greeter class.

lib/greeter.py

class Greeter
  def __init__(self, who):
    self.who = who

  def greet(self):
    print(f"Hello, {self.who}")

Execute:

git add lib/greeter.py
git commit -m "Add greeter class"

Changes for Greet: Modify the main program

Update the hello.py file to use greeter

lib/hello.py

import sys

from greeter import Greeter

name = sys.argv[0]
greeter = Greeter(name)
greeter.greet()

Execute:

git add lib/hello.py
git commit -m "Hello uses Greeter"

Up Next

We now have a new branch called greet with 2 new commits on it. Next we will learn how to navigate and switch between branches.