Checking Out a Local Branch
Here's how you can work independently from the "main" or "master" branch by checking out another local branch.
Checking Out a Local Branch featured image

Checking Out a Local Branch

Now that we have a clean working copy, the first thing we have to do is switch to (or "check out") our newly created branch:

$ git checkout contact-form
Concept

Checkout, HEAD, and Your Working Copy

A branch automatically points to the latest commit in that context. And since a commit references a certain version of your project, Git always knows exactly which files belong to that branch.

At each point in time, only one branch can be HEAD / checked out / active. The files in your working copy are those that are associated with this exact branch. All other branches (and their associated files) are safely stored in Git's database.

To make another branch (say, "contact-form") active, the "git checkout" command is used. This does two things for you:

  • (a) It makes "contact-form" the current HEAD branch.
  • (b) It replaces the files in your working directory to match exactly the revision that "contact-form" is at.

Running "git status" once more, you'll see that we're now "On branch contact-form". From now on, all of our changes and commits will only impact this very context - until we switch it again by using the "checkout" command to make a different branch active.

Let's prove this by creating a new file called "contact.html" and committing it:

$ git add contact.html
$ git commit -m "Add new contact form page"
$ git log
commit 56eddd14cf034f4bcb8dc9cbf847b33309fa5180
Author: Tobias Günther <support@learn-git.com>
Date: Fri Jul 26 10:56:16 2013 +0200

    Add new contact form page

commit 2dfe283e6c81ca48d6edc1574b1f2d4d84ae7f1
Author: Tobias Günther <support@learn-git.com>
Date: Fri Jul 26 10:52:04 2013 +0200

    Implement the new login box

commit 2b504bee4083a20e0ef1e037eea0bd913a4d56b6
Author: Tobias Günther <support@learn-git.com>
Date: Fri Jul 26 10:05:48 2013 +0200

    Change headlines for about and imprint

Looking at the Log, you'll see that your new commit was properly saved. No big surprises, so far. But now let's switch back to "master" and have a look at the Log once more:

$ git checkout master
$ git log
commit 2dfe283e6c81ca48d6edc1574b1f2d4d84ae7f1
Author: Tobias Günther <support@learn-git.com>
Date: Fri Jul 26 10:52:04 2013 +0200

    Implement the new login box

commit 2b504bee4083a20e0ef1e037eea0bd913a4d56b6
Author: Tobias Günther <support@learn-git.com>
Date: Fri Jul 26 10:05:48 2013 +0200

    Change headlines for about and imprint

You'll find that the "Add new contact form page" commit isn't there - because we made it in the context of our HEAD branch (which was the "contact-form" branch, not the "master" branch). This is exactly what we wanted: our changes are kept in their own context, separated from other contexts.

About Us

As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.

Just like with Tower, our mission with this platform is to help people become better professionals.

That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.