Skip to main content

GitKit (2nd ed.): Learn git and GitHub in Context

Section 3.3 Switching Branches

In the previous section you learned how to create a branch. In this section you will learn more about why branching is important and how to switch between existing branches.
Figure Figure 3.3.1 illustrates this idea by showing what what will happen when you switch to your feature branch and your Local Files have not been modified since the last commit on the main branch (drawn in green).
Cloud image of origin, upstream, and local branches (including new feature branch).
Figure 3.3.1. Switch to Feature Branch.

Exercises

1.

As you saw in Section 3.2, the git branch <name> command creates a branch (among other uses), but it does not change your active branch. The git switch <name> command switches (i.e. changes) the active branch.
(a)
    Type git switch <name> to change your active branch to the new feature branch that you created in the previous section. What output is produced?
  • Switched to branch ’<name>’
  • Correct!
  • Switched to branch ’main’
  • You were on main prior to switching branches.
  • error: Your local changes to the following files would be overwritten by checkout: ...
    Aborting
  • You should not have made any changes so far.
  • fatal: invlalid reference: <name>
  • You probably typed the branch name incorrectly. Run git branch again to see the name of the branch you created.
Hint.
Make sure you don’t include < > in your git switch <name> command.
(b)
    Which of the following commands could be used to confirm that your feature branch is now the active branch?
  • git branch
  • Correct!
  • git status
  • Correct!
  • git log
  • git log will show the commit history, it will not show the active branch
  • git branch <name>
  • When git branch ends with a name it creates a new branch, it does not show what is active.
Hint.
There are two commands that will provide information about the active branch.
(c)
    Which of the following commands could be used to make main the active branch?
  • git switch main
  • Correct!
  • git branch main
  • This will try to create a new branch named main.
  • git branch
  • git branch will show all the branches
  • git status
  • git status will show the active branch and information about changes
Hint.
Refer back to how you made your feature branch the active branch.
Type the command from Task 3.3.1.c and verify that main is the active branch.

2.

Assuming you are on the main branch, drag the steps that will make your feature branch the active branch and verify you are on your feature branch. Some steps may not be used.
Hint.
Think about how you make a branch active and then verify it.
Use the commands from Exercise 3.3.2 to make your feature branch the active branch and verify your active branch.
You have attempted 1 of 5 activities on this page.