Git Switch vs Git Checkout

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn the key differences between the git switch and git checkout commands. You will explore when and why to use each command, along with practical examples and use cases to help you streamline your Git workflow.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/BasicOperationsGroup -.-> git/status("Check Status") git/BranchManagementGroup -.-> git/branch("Handle Branches") git/BranchManagementGroup -.-> git/checkout("Switch Branches") subgraph Lab Skills git/status -.-> lab-391555{{"Git Switch vs Git Checkout"}} git/branch -.-> lab-391555{{"Git Switch vs Git Checkout"}} git/checkout -.-> lab-391555{{"Git Switch vs Git Checkout"}} end

Understanding Git Checkout

The git checkout command is a fundamental Git command used for switching branches or restoring working tree files. It updates the files in your working directory to match the version in the specified branch or commit.

Let's start by exploring how to use git checkout to switch between branches.

First, ensure you are in the project directory:

cd ~/project

Now, let's check the current branch. By default, after git init, you are on the main or master branch (depending on your Git configuration). In this lab's setup, we created a feature-branch.

git branch

You should see output similar to this, indicating the current branch with an asterisk:

  feature-branch
* main

Now, use git checkout to switch to the feature-branch:

git checkout feature-branch

You will see output confirming the switch:

Switched to branch 'feature-branch'

Let's verify the current branch again:

git branch

The output should now show that you are on feature-branch:

* feature-branch
  main

You have successfully used git checkout to switch branches.

Understanding Git Switch

The git switch command is a newer command introduced in Git 2.23, specifically designed for switching branches. It aims to provide a clearer separation of concerns compared to the overloaded git checkout command.

Let's use git switch to switch back to the main branch.

Ensure you are in the project directory:

cd ~/project

Now, use git switch to switch to the main branch:

git switch main

You will see output confirming the switch:

Switched to branch 'main'

Let's verify the current branch again:

git branch

The output should now show that you are on main:

* main
  feature-branch

You have successfully used git switch to switch branches. Notice that the output is similar to git checkout when switching branches.

Creating and Switching Branches with Git Switch

One of the convenient features of git switch is the ability to create a new branch and switch to it in a single command using the -c (or --create) option.

Let's create a new branch called development and switch to it.

Ensure you are in the project directory:

cd ~/project

Now, use git switch -c to create and switch to the development branch:

git switch -c development

You will see output indicating that a new branch was created and you switched to it:

Switched to a new branch 'development'

Let's verify the current branch and list all branches:

git branch

The output should show the new development branch and indicate that you are currently on it:

  feature-branch
  main
* development

This demonstrates how git switch -c simplifies the process of creating and immediately working on a new branch.

Restoring Files with Git Checkout

While git switch is primarily for switching branches, git checkout retains the functionality for restoring files. This is a key difference in their intended usage.

Let's make a change to file1.txt and then use git checkout to discard the changes and restore the file to its state in the current branch (development).

Ensure you are in the project directory:

cd ~/project

Add some content to file1.txt:

echo "Additional content" >> file1.txt

Check the status of the file:

git status

The output will show that file1.txt has been modified:

On branch development
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   file1.txt

no changes added to commit (use "git add" and/or "git commit -a")

Now, use git checkout -- to discard the local changes to file1.txt:

git checkout -- file1.txt

Check the status again:

git status

The output should now show that there are no changes in the working directory:

On branch development
nothing to commit, working tree clean

The changes you made to file1.txt have been discarded, and the file has been restored to its state in the development branch. This functionality is handled by git checkout, not git switch.

Checking Out a Specific Commit with Git Checkout

Another functionality retained by git checkout is the ability to check out a specific commit. This puts you in a "detached HEAD" state, allowing you to inspect the project at that point in history. git switch does not have this capability.

First, let's find the commit hash of the initial commit.

Ensure you are in the project directory:

cd ~/project

View the commit history:

git log --oneline

You will see output similar to this, with the commit hashes:

<commit_hash_development> (HEAD -> development) Initial commit
<commit_hash_main> (main, feature-branch) Initial commit

Note that the commit hashes will be different in your environment. Copy the commit hash for the "Initial commit".

Now, use git checkout followed by the commit hash to check out that specific commit. Replace <commit_hash> with the actual hash you copied.

git checkout <commit_hash>

You will see output indicating that you are in a detached HEAD state:

Note: switching to '<commit_hash>'

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>

HEAD is now at <commit_hash> Initial commit

You are now viewing the project as it was at the time of the initial commit. To return to a branch, you can use git switch or git checkout to switch back to a branch like development or main.

Let's switch back to the development branch using git switch:

git switch development

You will see output confirming the switch:

Switched to branch 'development'

You have successfully used git checkout to explore a specific commit and then returned to a branch using git switch.

Summary of Differences

To summarize the key differences between git checkout and git switch:

  • git switch: Primarily used for switching between branches. It is a newer, more focused command for this specific task. It can also create a new branch and switch to it (git switch -c).
  • git checkout: A more versatile command that can switch branches, but also has the ability to check out specific commits (resulting in a detached HEAD) and restore files (git checkout -- <file>).

While git checkout can perform branch switching, git switch is the recommended command for this purpose in newer Git versions due to its clearer intent and separation of concerns. Use git checkout when you need to restore files or explore specific commits.

You have now explored the core functionalities of both git checkout and git switch and understand when to use each command in your Git workflow.

Summary

In this lab, you have learned the differences between the git switch and git checkout commands. You practiced switching branches using both commands, creating a new branch with git switch, restoring files with git checkout, and checking out a specific commit with git checkout. You now understand the intended use cases for each command and how they contribute to an efficient Git workflow.