Skip to content

lab 27 Navigating Branches

Goals

You now have two branches in your project:

Execute:

git hist --all

Output:

$ git hist --all
* 18e7dbd 2022-10-24 | Updated Rakefile (HEAD -> greet) [Jim Weirich]
* 2a024f1 2022-10-24 | Hello uses Greeter [Jim Weirich]
* 85316c2 2022-10-24 | Added greeter class [Jim Weirich]
* 0022837 2022-10-24 | Added a Rakefile. (main) [Jim Weirich]
* 3172288 2022-10-24 | Moved hello.rb to lib [Jim Weirich]
* 3e56dbf 2022-10-24 | Add an author/email comment [Jim Weirich]
* 1dee7f9 2022-10-24 | Tell user how many names they have (tag: v1) [Jim Weirich]
* c72af6b 2022-10-24 | Rename variable to match its usage (tag: v1-beta) [Jim Weirich]
* 8cdd2cd 2022-10-24 | Can specify multiple names [Jim Weirich]
* 28fe396 2022-10-24 | Added a comment [Jim Weirich]
* 15c7573 2022-10-24 | Added a default value [Jim Weirich]
* 7d55044 2022-10-24 | Using ARGV [Jim Weirich]
* 91b926e 2022-10-24 | First Commit [Jim Weirich]

Switch to the Main Branch

Just use the git checkout command to switch between branches.

Execute:

git checkout main
cat lib/hello.rb

Output:

$ git checkout main
Switched to branch 'main'
$ cat lib/hello.rb
# Default is World
# Author: Jim Weirich (jim@somewhere.com)
names = ARGV || ["World"]

puts "Hello, #{names.join(" ")}!"
puts "You have #{names.length} names!"

You are now on the main branch. You can tell because the hello.rb file doesn’t use the Greeter class.

Switch Back to the Greet Branch.

Execute:

git checkout greet
cat lib/hello.rb

Output:

$ git checkout greet
Switched to branch 'greet'
$ cat lib/hello.rb
require 'greeter'

# Default is World
name = ARGV.first || "World"

greeter = Greeter.new(name)
puts greeter.greet

The contents of the lib/hello.rb confirms we are back on the greet branch.