Git Checkout Command – How To Switch To Branches and Commits

Git checkout

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, 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>

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) 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. 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).

If you don’t need cPanel, don't pay for it. Only pay for what you need with our scalable Cloud VPS Hosting.

check markCentOS, Debian, or Ubuntu check markNo Bloatware check markSSH and Root Access

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+