Navigating Branches

Goals

  • Learn how to navigate between the branches of a repository

You now have two branches in your project:

Execute:

  1. git hist --all

Output:

  1. $ git hist --all
  2. * 228a31e 2020-06-20 | Updated Rakefile (HEAD -> greet) [Jim Weirich]
  3. * 98cfd8a 2020-06-20 | Hello uses Greeter [Jim Weirich]
  4. * cf0438b 2020-06-20 | Added greeter class [Jim Weirich]
  5. * 5aec14d 2020-06-20 | Added a Rakefile. (master) [Jim Weirich]
  6. * 721b979 2020-06-20 | Moved hello.rb to lib [Jim Weirich]
  7. * 907a445 2020-06-20 | Add an author/email comment [Jim Weirich]
  8. * 4254c94 2020-06-20 | Added a comment (tag: v1) [Jim Weirich]
  9. * c8b3af1 2020-06-20 | Added a default value (tag: v1-beta) [Jim Weirich]
  10. * 30c2cd4 2020-06-20 | Using ARGV [Jim Weirich]
  11. * 4445720 2020-06-20 | First Commit [Jim Weirich]

Switch to the Master Branch

Just use the git checkout command to switch between branches.

Execute:

  1. git checkout master
  2. cat lib/hello.rb

Output:

  1. $ git checkout master
  2. Switched to branch 'master'
  3. $ cat lib/hello.rb
  4. # Default is World
  5. # Author: Jim Weirich (jim@somewhere.com)
  6. name = ARGV.first || "World"
  7. puts "Hello, #{name}!"

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

Switch Back to the Greet Branch.

Execute:

  1. git checkout greet
  2. cat lib/hello.rb

Output:

  1. $ git checkout greet
  2. Switched to branch 'greet'
  3. $ cat lib/hello.rb
  4. require 'greeter'
  5. # Default is World
  6. name = ARGV.first || "World"
  7. greeter = Greeter.new(name)
  8. puts greeter.greet

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