Getting Git

A comprehensive video course from git init to Git Master.

init: git checkout


In this video we'll learn how to use git checkout

The most common use for git checkout is to switch between branches.

We got a sneak peak of this in the master video for git branch, but let's demonstrate this again.

First, let's create a branch named new-branch by running:

git branch new-branch

Then, we'll switch to the new-branch by running:

git checkout new-branch

We can see that my prompt has changed from master to new-branch.

We can also verify this by running git status to see that we're on new-branch.

Now let's switch back to the master branch by running:

git checkout master

Since you'll usually create a new branch to starting new work, we can create and checkout a branch in one step with the -b option.

For example, I can run:

git checkout -b another-branch

As you can see, this created and switched me to another-branch.

There's one more important point to demonstrate about switching branches.

Since Git restores the state of the repository when switching branches, you often need to be in a clean state.

I'll demonstrate this by making some changes to File 2 and attempting the checkout the branch-1 branch.

Notice that Git throws a error with a warning that we have changes and need to either commit or discard them before switching branches.

While this only happens when there's a conflict, in this case, File 2 doesn't exist in branch-1, it's often good to be in a clean state before switching branches.