---
title: "Git Checkout Command &#8211; How To Switch To Branches and Commits"
description: "The “checkout” command in Git, or git checkout in practice, has many different uses throughout the life of a Git project. However, it is primarily used as a way of “checking out” different..."
url: https://www.inmotionhosting.com/support/website/git/git-checkout/
date: 2022-02-16
modified: 2025-02-20
author: "Christopher Maiorana"
categories: ["Git"]
type: post
lang: en
---

# Git Checkout Command &#8211; How To Switch To Branches and Commits

![Git checkout](https://www.inmotionhosting.com/support/wp-content/uploads/2022/02/Git-Checkout-Command-1024x538.png)

The “checkout” command in Git, or `git checkout` in practice, has many different uses throughout the life of a Git project. However, it is primarily used as a way of “checking out” different versions of your project. For example, if you want to look at a branch or a commit from some time in the past, the `checkout` command is the easiest way to do that.

## Checking Out a Branch With Git Checkout

As you may remember from the [full guide on branches](https://www.inmotionhosting.com/support/website/git/using-branches-in-git/), you can use the following command to view branches in your project:

git branch

You can use the `checkout` command to switch to any active branches:

git checkout <name of branch>

## Creating a New Branch with Git Checkout -b

Often, you’ll want to make any changes to your codebase in a new branch that you can pull later. Create a new branch with the following command:

git checkout -b <new branch name>

## Checkout a Commit (Detached HEAD)

Just as you can switch to different branches with the “checkout” command, you can also switch to commits. However, it’s important to note the difference between how commits and branches behave.

In Git, it’s very important to keep working in a linear fashion. Branches divert from the project timeline without disrupting the workflow. But commits are more like markers of progress that can reflect different points in time.

If you checkout a commit, you will be warned that you are in a “detached HEAD” state. This means that the HEAD reference point is no longer at the tip of the timeline but floating somewhere along the timeline. However, the detached HEAD will not damage your project.

To checkout a commit, you can run a command similar to the following:

git checkout <commit hash>

You do not need to repeat the full commit hash, you can use the first 5-7 characters, and that should be enough.

You will see a warning similar to the following:

Note: switching to '3c05bb0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>

Or undo this operation with:

git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 3c05bb0 Added shortcodes file

Once you are done “looking around” you can use the `git switch -` command (see [our full guide on the “switch” command](https://www.inmotionhosting.com/support/website/git/git-switch/)) to get back to the master branch.

## Using a Checkout to Publish or Move Files

The checkout command can also be used in a [hook](https://www.inmotionhosting.com/support/website/git/git-hooks/). The follow `bash` script will allow you to check out files to different locations.

#!/bin/sh
git --work-tree=/home/userna5/public_html --git-dir=/home/userna5/production.git checkout -f

In the above example, the `--work-tree` is the destination you would like to send the project files to, and the `--git-dir` is the location of the repository. This command will take the current project snapshot and match it to the “work-tree”. This is a great way to take files in your Git project and move them around between local or remote file systems. (See the [full guide on publishing files with Git](https://www.inmotionhosting.com/support/website/git/using-git-to-publish-files/)).

If you don’t need cPanel, don’t pay for it. Only pay for what you need with our scalable [Cloud VPS Hosting](https://www.inmotionhosting.com/cloud-vps).

![check mark](https://design.inmotionhosting.com/assets/icons/standard/check-blue.svg)CentOS, Debian, or Ubuntu ![check mark](https://design.inmotionhosting.com/assets/icons/standard/check-blue.svg)No Bloatware ![check mark](https://design.inmotionhosting.com/assets/icons/standard/check-blue.svg)SSH and Root Access
