lab 22 Creating a Branch
Goals
- Learn how to create a local branch in a repository
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.js
class Greeter { constructor(who) { this.who = who; } greet() { return `Hello, ${this.who}`; } } module.exports.Greeter = Greeter;
Execute:
git add lib/greeter.js git commit -m "Added greeter class"
Changes for Greet: Modify the main program
Update the hello.js file to use greeter
lib/hello.js
const { Greeter } = require('./greeter'); // Default is World const name = process.argv[2] || "World" const greeter = new Greeter(name); console.log(greeter.greet());
Execute:
git add lib/hello.js git commit -m "Hello uses Greeter"
Changes for Greet: Update the package.json
Update the package.json to pass a name argument to our program
package.json
{ "name": "greeter", "version": "1.0.0", "description": "Greet users from the command line", "scripts": { "start": "node lib/start.js 'Halle Bot'" } }
Execute:
git add package.json git commit -m "Updated package"
Up Next
We now have a new branch called greet with 3 new commits on it. Next we will learn how to navigate and switch between branches.